2

有人可以帮我..刚开始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);
    }
}   

}

4

8 回答 8

4

一些注意事项:

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);
}
于 2012-06-13T09:12:50.910 回答
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); 
    } 
于 2012-06-13T09:04:46.973 回答
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);
    }
}  
于 2012-06-13T09:06:22.573 回答
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);
}
}   
}
于 2012-06-13T09:04:09.550 回答
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);
   }   
}
于 2012-06-13T09:06:44.603 回答
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);
}
}   

 }
于 2012-06-13T09:08:50.623 回答
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);
}

}

于 2012-06-13T09:13:16.547 回答
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();
}

}
于 2014-09-26T05:04:26.440 回答