我该怎么做才能让我的代码显示一些线条(就像它一样)但向后显示

时间:2016-11-02 23:55:54

标签: java string for-loop count int

所以当我输入4,而不是说4瓶啤酒等时,它将从100开始然后转到96然后停止(以最后一行结束)

import java.util.Scanner;

public class ThreeDotNine {
  public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.print("How many Verses would you like to print:  ");
      String num = sc.nextLine();
      int num2 = Integer.parseInt(num);
      String s = " ";

      for (int x = num2; x > 0; x--) {
          System.out.println(x + " bottles of beer on the wall " + x + " bottles of beer");
          System.out.println("Take one down, pass it around, " + (x - 1) + " bottles of beer on the wall.\n");
      }
      System.out.print("Go to the store, buy some more, ");
      System.out.println("99 bottles of beer on the wall.\n");

  }
}

2 个答案:

答案 0 :(得分:0)

该计划没有任何问题。它工作正常,只需再次检查

How many Verses would you like to print:  4
4 bottles of beer on the wall 4 bottles of beer
Take one down, pass it around, 3 bottles of beer on the wall.

3 bottles of beer on the wall 3 bottles of beer
Take one down, pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall 2 bottles of beer
Take one down, pass it around, 1 bottles of beer on the wall.

1 bottles of beer on the wall 1 bottles of beer
Take one down, pass it around, 0 bottles of beer on the wall.

Go to the store, buy some more, 99 bottles of beer on the wall.

答案 1 :(得分:-1)

我会这样尝试:

for ( int x = 0; x <= num2; x++ ) {
  int y = 100 - x;

  System.out.println(y + " bottles of beer on the wall " + y + " bottles of beer");
  System.out.println("Take one down, pass it around, " + (y - 1) + " bottles of beer on the wall.\n");
}

如果定义第二个变量,则更容易理解。

现在你有了输出 100 99 98 97 96