2013-02-17 79 views
0

我对编程非常陌生,并试图掌握如何在Java中进行倒计时的想法。Java计数器循环

我想运行,需要你以这种形式进入5首歌曲的程序:

Please enter song 1: "Thriller" 
Choose 4 more songs. 

所以我指望从5回1,我可以得到关于如何做到这一点的一些想法?这是我下面可怜的尝试,但注意我的柜台增加了(这不是我想要的)。

import java.util.Scanner; 

public class testing { 
    public static void main(String[] args) { 
     String song; 
     int amount = 0; 
     Scanner kdb = new Scanner(System.in); 
     while (amount < 5) { 
      System.out.println("enter song:"); 
      song = kdb.next(); 
      amount++; 
      System.out.println("you chose " + song + amount + " more required"); 
     } 
    } 
} 
+1

,而不是递增,递减然后,改变while循环条件。 – 2013-02-17 18:55:19

+0

尝试'量 - '? – 2013-02-17 18:55:59

+0

amunt--代替金额++ – Marc 2013-02-17 18:56:37

回答

1

变化amount++(同amount = amount + 1)。它应该是amount--(与amount = amount - 1相同)。

重写程序(带for环路):

import java.util.Scanner; 

public class Test { 
    public static void main(String... args) { 
     String song; 
     Scanner kdb = new Scanner(System.in); 
     for (int amount = 5; amount > 0; amount--) { 
      System.out.println("Enter song:"); 
      song = kdb.next(); 
      System.out.println("You chose " + song + ". Choose" + amount + " more"); 
     } 
    } 
} 
+1

你确定你没有跑进'无限循环'吗? – 2013-02-17 19:03:41

+1

量= 5;金额> 0; - 数量 – Marc 2013-02-17 19:04:32

+0

我的意思是说,与OP的要求有点混淆。 – syb0rg 2013-02-17 19:06:07

0

这里是我会怎么做,

public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     String song; 
     int amount = 1; 
     Scanner kdb = new Scanner(System.in); 
     while (amount <= 5) { 
      System.out.println("enter song:"); 
      song = kdb.next(); 
      if(amount<5){ 
      System.out.println("you chose " + song + amount + " more required"); 
      } 
      ++amount; 
     } 

    }