检查整数的数目是否在增加(java)
我想做的应该很简单,但由于我对java还不熟悉,我正在为可能是基本编程而挣扎 主要问题是如何检查整数的(x+1)数是否大于x数,我将尝试如下操作:检查整数的数目是否在增加(java),java,arrays,integer,Java,Arrays,Integer,我想做的应该很简单,但由于我对java还不熟悉,我正在为可能是基本编程而挣扎 主要问题是如何检查整数的(x+1)数是否大于x数,我将尝试如下操作: for( int x=0; x < Integer.toString(numblist).length();x++) { if (numblist[x] < numblist[x+1]) { compliance= "OK"; } else{ compliance="
for( int x=0; x < Integer.toString(numblist).length();x++) {
if (numblist[x] < numblist[x+1]) {
compliance= "OK";
} else{
compliance="NOK";
}
for(int x=0;x
但它返回一个错误“需要数组,但找到整数”。
这似乎是一个基本类型错误,可能来自上一步(仅保留字符串中包含的数字):
for(int p=0;p
我在网上找不到答案,事实上答案并不复杂,这让我抓狂,如果有人能帮助我,我将不胜感激!您不能使用
[]索引整数(即numblist
)
syntax——这只适用于数组,因此是错误的。我认为这比必须的更复杂;为什么不从整数的后面开始,检查数字是否在减少,这样可以避免所有与字符串有关的事情:
int n = numblist;
boolean increasing = true;
while (n > 0) {
int d1 = n % 10;
n /= 10;
int d2 = n % 10;
if (d2 > d1) {
increasing = false;
break;
}
}
您不能使用
[]
语法为整数(即numblist
)编制索引,这只适用于数组,因此您会犯错误。我认为这比必须的更复杂;为什么不从整数的后面开始,检查数字是否在减少,这样可以避免使用字符串的所有操作:
int n = numblist;
boolean increasing = true;
while (n > 0) {
int d1 = n % 10;
n /= 10;
int d2 = n % 10;
if (d2 > d1) {
increasing = false;
break;
}
}
做相反的操作。如果它们从第一个数字开始增加,则意味着它们从最后一个数字减少到第一个数字。这样编程更容易:
public boolean increasingDigits(int input)
{
// Deal with negative inputs...
if (input < 0)
input = -input;
int lastSeen = 10; // always greater than any digit
int current;
while (input > 0) {
current = input % 10;
if (lastSeen < current)
return false;
lastSeen = current;
input /= 10;
}
return true;
}
公共布尔递增数字(int输入)
{
//处理负面输入。。。
如果(输入<0)
输入=-输入;
int lastSeen=10;//始终大于任何数字
电流;
while(输入>0){
电流=输入%10;
如果(上次看到<当前)
返回false;
lastSeen=电流;
输入/=10;
}
返回true;
}
做相反的操作。如果它们从第一个数字开始增加,则意味着它们从最后一个数字减少到第一个数字。这样编程更容易:
public boolean increasingDigits(int input)
{
// Deal with negative inputs...
if (input < 0)
input = -input;
int lastSeen = 10; // always greater than any digit
int current;
while (input > 0) {
current = input % 10;
if (lastSeen < current)
return false;
lastSeen = current;
input /= 10;
}
return true;
}
公共布尔递增数字(int输入)
{
//处理负面输入。。。
如果(输入<0)
输入=-输入;
int lastSeen=10;//始终大于任何数字
电流;
while(输入>0){
电流=输入%10;
如果(上次看到<当前)
返回false;
lastSeen=电流;
输入/=10;
}
返回true;
}
我假设您正在检查整数中的各个数字。如果是这样,最好将整数转换为字符串,然后循环遍历字符串中的字符
public class Test {
public static void main(String[] args) {
Integer num = 234; // New Integer
String string = num.toString(); // Converts the Integer to a string
// Loops through the characters in the string
for(int x = 0; x < string.length() - 1; x++){
// Makes sure that both string.charAt(x) and string.charAt(x+1) are integers
if(string.charAt(x) <= '9' && string.charAt(x) >= '0' && string.charAt(x+1) <= '9' && string.charAt(x+1) >= '0'){
if(Integer.valueOf(string.charAt(x)) < Integer.valueOf(string.charAt(x+1))){
System.out.println("OK");
}else{
System.out.println("NOK");
}
}
}
}
}
公共类测试{
公共静态void main(字符串[]args){
整数num=234;//新整数
String String=num.toString();//将整数转换为字符串
//循环遍历字符串中的字符
对于(int x=0;x
我假设您正在检查整数中的各个数字。如果是这样,最好将整数转换为字符串,然后循环遍历字符串中的字符
public class Test {
public static void main(String[] args) {
Integer num = 234; // New Integer
String string = num.toString(); // Converts the Integer to a string
// Loops through the characters in the string
for(int x = 0; x < string.length() - 1; x++){
// Makes sure that both string.charAt(x) and string.charAt(x+1) are integers
if(string.charAt(x) <= '9' && string.charAt(x) >= '0' && string.charAt(x+1) <= '9' && string.charAt(x+1) >= '0'){
if(Integer.valueOf(string.charAt(x)) < Integer.valueOf(string.charAt(x+1))){
System.out.println("OK");
}else{
System.out.println("NOK");
}
}
}
}
}
公共类测试{
公共静态void main(字符串[]args){
整数num=234;//新整数
String String=num.toString();//将整数转换为字符串
//循环遍历字符串中的字符
对于(int x=0;x
私有布尔值isIncreasingOrder(int num){
字符串值=整数.toString(num);
返回IntStream.range(0,value.length()-1).noneMatch(i->Integer.parseInt(value.substring(i,i+1))>Integer.parseInt(value.substring(i+1,i+2));
}
私有布尔值isIncreasingOrder(int num){
字符串值=整数.toString(num);
返回IntStream.range(0,value.length()-1).noneMatch(i->Integer.parseInt(value.substring(i,i+1))>Integer.parseInt(value.substring(i+1,i+2));
}
我认为一个简单的方法就是这样
package javacore;
import java.util.Scanner;
// checkNumber
public class Main_exercise4 {
public static void main (String[] args) {
// Local Declarations
int number;
boolean increasingNumber=false;
Scanner input = new Scanner(System.in);
number = input.nextInt();
increasingNumber = checkNumber(number);
System.out.println(increasingNumber);
}
public static boolean checkNumber(int number) {
boolean increasing = false;
while(number>0) {
int lastDigit = number % 10;
number /= 10;
int nextLastDigit = number % 10;
if(nextLastDigit<=lastDigit) {
increasing=true;
}
else {
increasing=false;
break;
}
}
return increasing;
}
}
包javacore;
导入java.util.Scanner;
//支票号码
公共课主要练习4{
公共静态void main(字符串[]args){
//本地声明
整数;
布尔递增数=false;
扫描仪输入=新扫描仪(System.in);
number=input.nextInt();
递增编号=支票编号(编号);
System.out.println(递增编号);
}
公共静态布尔校验号(整数){
布尔递增=假;
而(数量>0){
int lastDigit=数字%10;
数目/=10;
int nextLastDigit=编号%10;
如果(nextLastDigit我想