0

我对编程非常陌生,并试图掌握如何在 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");
        }
    }
}
4

2 回答 2

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");
        }
    }
}
于 2013-02-17T18:59:56.163 回答
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;
        }

    }
于 2013-02-17T19:06:26.607 回答