刚刚开始使用Java,这是适用于墙上99瓶啤酒的合适解决方案吗?

时间:2016-06-08 03:22:16

标签: java java.util.scanner do-while

问题的链接:http://rosettacode.org/wiki/99_Bottles_of_Beer

我的代码:

import java.util.Scanner;

public class Application {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the number of bottles of beer on the wall: ");

    int X = input.nextInt();

    do {
        if (X == 1) {
            System.out.println(X + " bottle of beer on the wall");
            System.out.println(X + " bottle of beer");
            System.out.println("Take one down, pass it around");
            System.out.println(X-- + " bottle of beer on the wall");
            System.out.println();
        } else {
            System.out.println(X + " bottles of beer on the wall");
            System.out.println(X + " bottles of beer");
            System.out.println("Take one down, pass it around");
            System.out.println(X-- + " bottles of beer on the wall");
            System.out.println();
        }
    } while (X >= 0);
}

}

有更好的方法吗?我在学校的递归上已经触及了一点,但还不知道如何用Java实现它。有什么建议吗?

3 个答案:

答案 0 :(得分:0)

因为你知道瓶子的数量,我会使用for循环而不是do while。由于输入的瓶子数量可能是1,我会使用一段时间而不是do / while(如果您希望它至少发生一次,请使用do / while)。

在风格上,X应该是x,或者更好的是numberOfBottles。

我也移动了:

csv

在if / else之外,因为它们都是相同的。

最后,我还要将System.out.println(); 单独移动到一行,以便人们不必考虑X--在打印之前或之后发生。

答案 1 :(得分:0)

这是一个非常容易植入的for循环。注意:我使用了" \ n"这是你打印到控制台后如何做一个新行,以防你不熟悉

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the number of bottles of beer on the wall: ");

    int numOfBottles = input.nextInt();

    for(int i = numOfBottles; i >= 1; i--){
        System.out.println(i + " bottle of beer on the wall");
        System.out.println(i + " bottle of beer");
        System.out.println("Take one down, pass it around");
        System.out.println(i-1 + " bottle of beer on the wall\n");

}

答案 2 :(得分:0)

尝试这个do-while循环: -

XSSFWorkbook wb = new XSSFWorkbook(); 
XSSFSheet sheet = wb.createSheet("SomeSheet");

Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
XSSFDataValidationHelper dvHelper = new SSFDataValidationHelper(sheet);

XSSFDataValidationConstraint dvConstraint = 
    (XSSFDataValidationConstraint)
    dvHelper.createNumericConstraint(
        XSSFDataValidationConstraint.ValidationType.DECIMAL,
        XSSFDataValidationConstraint.OperatorType.BETWEEN,
        String.valueOf(Float.MIN_VALUE),
        String.valueOf(Float.MAX_VALUE)
    );

// Cell range is important here. 
CellRangeAddressList addressList = new CellRangeAddressList(
        0, 2, 1, 3);
// 0 - starting row, 2 - ending row
// 1 - starting col, 3 - ending col

XSSFDataValidation validation =(XSSFDataValidation)dvHelper.createValidation(
        dvConstraint, addressList);
validation.setSuppressDropDownArrow(false);
validation.setShowErrorBox(true);

CellStyle style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_LEFT);
cell.setCellStyle(style);

sheet.addValidationData(validation);

cell.setCellValue(20);