从用户处读取正数,直到他们输入负数。打印出已读取的最大正数。 Java

huangapple go评论59阅读模式
英文:

Read in positive numbers from the user until they enter a negative number. Print out the largest positive number that was read. Java

问题

以下是翻译好的部分:

import java.util.Scanner;

class Main {

  public static void main(String[] args) {

    int counter = 0;
    Scanner sc = new Scanner(System.in);

    int number = sc.nextInt();

    while (number >= 0) {
      counter = counter + number;
      number = sc.nextInt();
    }
    System.out.println(counter);

  }
}
英文:

So like, I'm new to Java and whatnot and for the most part, it's been pretty alright. However, I'm kind of stumped with this one problem. So basically I have to write a program in a conditional loop where it reads user input: You enter positive numbers until you enter a negative number, where the program then prints out the largest positive number in that.

I'm building this off of another program where the program read in positive numbers until you entered a negative one in which the program would then print out the sum.

I don't really know what to do past this, can anyone help?

Here's my code

import java.util.Scanner;

class Main {

  public static void main(String[] args) {

    int counter=0;
    Scanner sc=new Scanner(System.in);
    
    int number=sc.nextInt();
    
    while(number>=0){
      counter=counter+number;
      number=sc.nextInt();
    }
    System.out.println(counter);
    
  }
}

答案1

得分: 0

the quickest way to request user input off the bat, just like that, is the prompt function.. welcome to javascript :}

<!-- begin snippet: js hide: false console: true babel: false -->

var n=0;
var arr=[];
while(n!=-1){
  var m=parseInt(prompt("Enter a number\nEnter value -1 to end entries")); //parseInt attempts to turn the data input into a number and prompt returns what the user inputs
  if(!isNaN(m)){//i'm asking if m is a number
    if(m!=-1){arr.push(m);}//this ensures that when -1 is inputted, it isn't a part of the values sorted through
    n=m;
  }
}
alert(arr.sort().reverse()); //sort sorts from highest to lowest, reversing such would do the exact opposite

<!-- end snippet -->

英文:

the quickest way to request user input off the bat, just like that, is the prompt function.. welcome to javascript :}

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var n=0;
var arr=[];
while(n!=-1){
  var m=parseInt(prompt(&quot;Enter a number\nEnter value -1 to end entries&quot;)); //parseInt attempts to turn the data input into a number and prompt returns what the user inputs
  if(!isNaN(m)){//i&#39;m asking if m is a number
    if(m!=-1){arr.push(m);}//this ensures that when -1 is inputted, it isn&#39;t a part of the values sorted through
    n=m;
  }
}
alert(arr.sort().reverse()); //sort sorts from highest to lowest, reversing such would do the exact opposite

<!-- end snippet -->

答案2

得分: 0

你离成功不远...

创建一个变量来保存最大值,然后在每次循环迭代时检查输入的数字是否大于最大值。如果是,则进行更新。

import java.util.Scanner;

class Main {

        public static void main(String[] args) {

                int largest = 0;
                Scanner sc = new Scanner(System.in);

                int number = sc.nextInt();
                while (number >= 0) {
                        if (number > largest) {
                                largest = number;
                        }
                        number = sc.nextInt();
                }
                System.out.println(largest);
        }
}
英文:

You were close ...

Create a variable to hold the largest value, then on each loop iteration check if the number entered is greater than the largest value. If so, update it.

import java.util.Scanner;
  
class Main {

        public static void main(String[] args) {

                int largest=0;
                Scanner sc=new Scanner(System.in);

                int number=sc.nextInt();
                while(number&gt;=0){
                        if (number &gt; largest) {
                                largest = number;
                        }
                        number=sc.nextInt();
                }
                System.out.println(largest);
        }
}

huangapple
  • 本文由 发表于 2020年10月24日 07:46:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64508585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定