有人可以帮我吗...刚开始java .. :( 如何根据给定的最小输入值,最大输入值和递增值显示所有可能的值?
例如: 最小值:1 最大值:10 增量值:2
结果将是:1,3,5,7,9
这是我到目前为止所得到的......
public class DisplayIncrement {
public static void main(String []args){
int min, max, increment;
Scanner in = new Scanner(System.in);
System.out.println("Enter min value: ");
in.nextInt();
System.out.println("Enter max value: ");
in.nextInt();
System.out.println("Enter increment value: ");
in.nextInt();
int i;
for(i=0; i<=10; i+=2){
System.out.println(i);
}
}
}
答案 0 :(得分:3)
一些注释:
1- in.nextInt();
从用户读取一个整数,阻塞直到用户在控制台中输入一个整数并按 ENTER 。必须保存结果整数才能在以后使用它,并将其保存到变量中,如下所示:
int value = in.nextInt();
在您的代码中,您需要将用户输入的3个整数分配给相应的变量:
System.out.println("Enter min value: ");
min = in.nextInt();
System.out.println("Enter max value: ");
max = in.nextInt();
System.out.println("Enter increment value: ");
increment = in.nextInt();
2-您正在很好地实现循环,但您只需要使用用户的输入而不是使用显式整数:
for(int i = min; i <= max; i += increment)
{
System.out.println(i);
}
答案 1 :(得分:1)
System.out.println("Enter min value: ");
int minVal = in.nextInt();
System.out.println("Enter max value: ");
int maxVal = in.nextInt();
System.out.println("Enter increment value: ");
int increment = in.nextInt();
for(i=minVal; i<=maxVal; i+=incremement){
System.out.println(i);
}
答案 2 :(得分:1)
首先,你声明了所需的整数,但实际上并没有使用它们。
因此,当您调用方法in.nextInt();
时,您实际上是“获取”一个整数。因此,您可以将整数min,max和increment设置为该方法。像这样,
System.out.println("Enter min value: ");
min = in.nextInt();
System.out.println("Enter max value: ");
max = in.nextInt();
System.out.println("Enter increment value: ");
increment = in.nextInt();
因此,对于完整的答案,这样的事情应该有效。
public class DisplayIncrement {
public static void main(String []args){
int min, max, increment;
Scanner in = new Scanner(System.in);
System.out.println("Enter min value: ");
min = in.nextInt();
System.out.println("Enter max value: ");
max = in.nextInt();
System.out.println("Enter increment value: ");
increment = in.nextInt();
for(int i=min; i<=max; i+=increment){
System.out.println(i);
}
}
答案 3 :(得分:0)
public class DisplayIncrement {
public static void main(String []args){
int min, max, increment;
Scanner in = new Scanner(System.in);
System.out.println("Enter min value: ");
int min=in.nextInt();
System.out.println("Enter max value: ");
int max=in.nextInt();
System.out.println("Enter increment value: ");
int inc=in.nextInt();
int i;
for(i=min; i<max; i+=inc){
System.out.println(i);
}
}
}
答案 4 :(得分:0)
代码中的缺陷很明显但是......
public class DisplayIncrement {
public static void main(String []args) {
int min, max, increment;
Scanner in = new Scanner(System.in);
System.out.println("Enter min value: ");
min = in.nextInt();
System.out.println("Enter max value: ");
max = in.nextInt();
System.out.println("Enter increment value: ");
increment = in.nextInt();
for(int i = min; i <= max; i += increment)
System.out.println(i);
}
}
答案 5 :(得分:0)
public class DisplayIncrement {
public static void main(String []args){
int min, max, increment;
Scanner in = new Scanner(System.in);
System.out.println("Enter min value: ");
min = in.nextInt();
System.out.println("Enter max value: ");
max = in.nextInt();
System.out.println("Enter increment value: ");
increment = in.nextInt();
for(; min<=max; min+=increment ){
System.out.println(min);
}
}
}
答案 6 :(得分:0)
public static void main(String []args){
int min, max, increment;
Scanner in = new Scanner(System.in);
System.out.println("Enter min value: ");
int minval=in.nextInt();
System.out.println("Enter max value: ");
int maxval=in.nextInt();
System.out.println("Enter increment value: ");
int incr=in.nextInt();
int i;
for(i=minval; i<=maxval;i+=incr){
System.out.println(i);
}
}
答案 7 :(得分:0)
不要忘记最后关闭扫描仪。
import java.util.Scanner;
public class DisplayIncrement{
public static void main(String[] args) {
int min, max, increment;
Scanner in = new Scanner(System.in);
System.out.println("Enter min value: ");
min = in.nextInt();
System.out.println("Enter max value: ");
max = in.nextInt();
System.out.println("Enter increment value: ");
increment = in.nextInt();
//for(initial value; condition; increment/decrement)
for(int i=min; i <=max; i+= increment){
System.out.println(i);
}
in.close();
}
}