Java 然后,Input number继续读取int.值,直到这些整数之和等于或大于开始时输入的值
我尝试过这个逻辑,但它不起作用,请帮助我解决这个问题Java 然后,Input number继续读取int.值,直到这些整数之和等于或大于开始时输入的值,java,Java,我尝试过这个逻辑,但它不起作用,请帮助我解决这个问题 import java.util.Scanner ; // USing Scanner class class Check { public static void main(String args[]){ //Intializing & Declaring Variables int num ; int sum ; int x = 1; // Creating object of Scanner Class
import java.util.Scanner ; // USing Scanner class
class Check {
public static void main(String args[]){
//Intializing & Declaring Variables
int num ;
int sum ;
int x = 1;
// Creating object of Scanner Class
Scanner input = new Scanner(System.in);
while(x <= 100){
System.out.println("Enter an Integer"); //Prompt
num = input.nextInt(); // Taking input from User
sum = num + num ;
x = sum;
} // while lop ends
} // mainMethod ends
import java.util.Scanner;//使用Scanner类
课堂检查{
公共静态void main(字符串参数[]){
//初始化和声明变量
int-num;
整数和;
int x=1;
//创建Scanner类的对象
扫描仪输入=新扫描仪(System.in);
而(x首先,提示用户输入目标值(而不是硬编码100
)。其次,将num
添加到sum
(而不是num
到num
)。第三,我将显示运行总数。第四,请选择有意义的变量名称。例如
Scanner input = new Scanner(System.in);
// Prompt the user for a "target value"
System.out.println("Enter target value");
int target = input.nextInt();
// Initialize a running sum
int sum = 0;
// While the sum is less than or equal to the target value
while (sum <= target) {
// Prompt the user for a value
System.out.println("Enter an Integer");
// Add the value to the sum
sum += input.nextInt();
// Display the current sum
System.out.println("The current sum is " + sum);
}
// Indicate loop has completed
System.out.println("Done.");
扫描仪输入=新扫描仪(System.in);
//提示用户输入“目标值”
System.out.println(“输入目标值”);
int target=input.nextInt();
//初始化运行总和
整数和=0;
//当总和小于或等于目标值时
而