gpt4 book ai didi

java - 未使用的声明使我的程序正常工作吗?

转载 作者:行者123 更新时间:2023-12-02 12:12:26 25 4
gpt4 key购买 nike

所以我编写了这段代码,并在其中编写了“int number = input.nextInt();”作为实验。该程序完全按照我的要求执行,但我不明白为什么会这样。该变量稍后在程序中根本不会使用,但如果我删除它,程序将停止工作。有任何想法吗?我的代码如下:

import java.util.Scanner;
/*
* Name: Ki
*/
public class Countdown {
public static void main(String[] args) {
//Create a Scanner object to accept the input from user
Scanner input = new Scanner(System.in);

// Prompt the user to enter starting number, I just set it to 5 because
// that's what the program had as standard input.
System.out.println("Enter the beginning number: ");

// I don't know how this works. I had this here as an experiment, but
// the program now doesn't work without it so I guess I'll leave it here.
int number = input.nextInt();

// sets max countdown value
int i=5;

// Make a loop that makes the program countdown until it reaches 2
while(i>1)
{
System.out.print(i + " ... \n");
i--;
}
// Makes the program print 1 without dots and print stopped at the end
if (i==1) System.out.println(i);
System.out.print("Stopped");
}
}

最佳答案

 int number = input.nextInt();

// sets max countdown value


// Make a loop that makes the program countdown until it reaches 2
while(number>1)
{
System.out.print(number + " ... \n");
number--;
}
// Makes the program print 1 without dots and print stopped at the end
if (number==1) System.out.println(number);
System.out.print("Stopped");

关于java - 未使用的声明使我的程序正常工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46438107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com