接受int值的Java程序,直到用户输入零,然后找到最小正整数

时间:2017-02-19 01:51:37

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

如何确保最低限度不包括0?我已经确定答案不是负数,但现在我不知道如何在按下0时输入0作为输入以找到最小值。

import java.util.Scanner;
public class Q2
{
public static void main (String [] args)
{
    System.out.println("Write a list of integers and type 0 when you are finished");
    Scanner kb=new Scanner(System.in);
    int input=kb.nextInt();
    int smallestValue=Integer.MAX_VALUE; 
    boolean negative=false;
    do{
        input=kb.nextInt();
        if(!negative){
           smallestValue=input;
           negative=false;
        }
        if (input<smallestValue){
           smallestValue=input;
        }
    }
    while(input!=0);
    System.out.println(smallestValue+">0");
  }
}       

3 个答案:

答案 0 :(得分:1)

我建议使用while循环而不是dowhile循环

System.out.println("Write a list of integers and type 0 when you are finished");
Scanner kb=new Scanner(System.in);
int input=kb.nextInt();
int smallestValue=Integer.MAX_VALUE; 

while(input!=0){//loop until user inputs 0
    if(input>0) {//check for inputs greater than 0 and ignore -ve values
        if(input<smallestValue) // save the smallest value
            smallestValue = input;
    }
    input=kb.nextInt();
}
System.out.println(smallestValue+">0");

答案 1 :(得分:0)

测试它不是mysqldump --skip-lock-tables --single-transaction --flush-logs --hex-blob --master-data=2 --user=myuser --password=[password] --host=127.0.0.1 mydb | tee >(mysql --max_allowed_packet=128M -h 192.168.1.110 -u myuser -p[password] otherdb) >(head -30 >/tmp/pointintime.log) 小于def openfiles(self, *args): satter2 = BoxLayout(pos= (629, 950), size_hint= (.1,.1)) self.fclv = FileChooserListView(path= '/sdcard/', filters= [‘*.3gp’]) self.fclv.bind(on_selection= self.pressed(fclv.selection) scatter.add_widget(self.fclv) self.add_widget(satter2) def pressed(self, filename): #with open(os.path.join(path, filename[0])) if self.soundf is None: self.soundf = SoundLoader.load(self.path) if self.soundf.status != 'stop': self.soundf.stop() self.soundf.loop = False self.soundf.play() 。变化

0

smallestValue

但是,你的算法的其余部分似乎有点缺陷。我想你想要,

if (input<smallestValue){

您目前也放弃了第一个值。一个完整的工作示例

if (input != 0 && input<smallestValue){

可以也将内部// if(!negative){ // smallestValue=input; // negative=false; // } if (input > 0) { smallestValue = Math.min(smallestValue, input); } 写为

public static void main(String[] args) {
    System.out.println("Write a list of integers and type 0 when you are finished");
    Scanner kb = new Scanner(System.in);
    int smallestValue = Integer.MAX_VALUE;
    int input;
    do {
        input = kb.nextInt();
        if (input > 0) {
            smallestValue = Math.min(input, smallestValue);
        }
    } while (input != 0);
    System.out.println(smallestValue + " > 0");
}

答案 2 :(得分:0)

使用Do-While循环的解决方案:

import java.util.Scanner;

public class Task3 {

    public static void main(String[] args) {
        System.out.println("Write a list of integers and type 0 when you are finished");
        Scanner kb = new Scanner(System.in);
        int input = kb.nextInt();
        int smallestValue = Integer.MAX_VALUE;
        do {
            if (input < smallestValue)
                smallestValue = input;
            input = kb.nextInt();
        } while (input != 0);
        System.out.println(smallestValue + ">0");
    }

}