[英]Branching statements - ascending or descending integers
Been studying Java for a few months now with no prior development experience, but I am determined to learn this.已经学习 Java 几个月了,之前没有开发经验,但我决心学习这个。 This is my first post.
这是我的第一篇文章。 Here's the question:
这是问题:
Write a program that reads a sequence of integer numbers and outputs true if the sequence is ordered (in ascending or descending order), otherwise, false.编写一个程序,读取 integer 数字序列,如果序列是有序的(按升序或降序)输出 true,否则为 false。 Keep in mind, if a number has the same value as the following number, it does not break the order.
请记住,如果一个数字与后面的数字具有相同的值,它不会破坏顺序。 The sequence ends with 0. Do not consider this number as a part of the sequence.
序列以 0 结尾。不要将此数字视为序列的一部分。 The sequence always has at least one number (excluding 0).
序列总是至少有一个数字(不包括 0)。
Sample Input 1: 9 8 7 6 5 4 3 2 1 0样本输入 1:9 8 7 6 5 4 3 2 1 0
Sample Output 1: true样品 Output 1:真
Sample Input 2: 1 2 3 3 9 0样本输入 2:1 2 3 3 9 0
Sample Output 2: true样品 Output 2:真
Sample Input 3: 1 2 5 5 2 3 0样本输入 3:1 2 5 5 2 3 0
Sample Output 3: false样品 Output 3:假
I've read other posts that addresses this issue, but my code is a representation of the concepts I've learned and quasi-understand thus far.我已经阅读了解决此问题的其他帖子,但我的代码代表了我迄今为止所学和准理解的概念。 My intuition is that in order to iterate through all the values that are input BEFORE producing a 'true' or 'false', a list is probably needed.
我的直觉是,为了在产生“真”或“假”之前遍历所有输入的值,可能需要一个列表。 But, I'm unsure of how to utilize Scanner within a list and how to use a list within a 'for loop' &/or 'if' statements.
但是,我不确定如何在列表中使用 Scanner 以及如何在“for 循环”和/或“if”语句中使用列表。 Here is my code thus far, which is producing a error of time limit exceeded.
到目前为止,这是我的代码,它产生了超出时间限制的错误。 Thank you in advance for your help.
预先感谢您的帮助。
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
for (int i = 1; i > 0; i++) {
if (input >= i || input == i + 1) {
System.out.println(true);
}
else if (input <= i || input == i - 1) {
System.out.println(true);
}
else {
System.out.println(false);
}
}
}
}
Let's first read in all the numbers:让我们首先阅读所有数字:
Scanner scanner = new Scanner(System.in);
List<Integer> numbers = new ArrayList<>(); //use a list to store
int number = -1;
while(number != 0) { //go until 0
System.out.println("Enter a positive int or 0 to stop!");
number = scanner.nextInt(); //read integer
if(number != 0) {
numbers.add(number); //save it, but only if it's not 0
}
}
Now we have all numbers in the list.现在我们在列表中有所有数字。 Some methods can be writter to check if they are ascending:
可以编写一些方法来检查它们是否在升序:
public static boolean isAscending(List<Integer> numbers) {
int number, next;
if(numbers.size()==1) return true; // edge cases
if(numbers.size()==2) {
return numbers.get(0) <= numbers.get(1);
}
for(int i=0; i<numbers.size() - 2; i++) { //from first to one before last
number = numbers.get(i);
next = numbers.get(i+1);
if(next < number) { //next is NOT bigger then the current (equal accepted)
return false;
}
}
return true; //if we got here then it's ascending
}
The descending method is similar, but you use '>' when comparing the numbers.降序方法类似,但在比较数字时使用“>”。 Then, it's just a matter of calling them on the input list:
然后,只需在输入列表中调用它们即可:
boolean ascending = isAscending(numbers);
boolean descending = isDescending(numbers);
if(ascending || descending) {
//result is TRUE here
System.out.println("TRUE");
} else {
//result is FALSE here
System.out.println("FALSE");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.