[英]I'm having issues with leading zeros
I'm attempting to get this to recognize the leading zeros in my program, and I thought using 'String.format("%03d", code);' 我试图让它识别程序中的前导零,并且我认为使用'String.format(“%03d”,code);' would take care of it but I'm still not getting the expected result. 会解决的,但我仍然没有得到预期的结果。
import java.util.Scanner;
import java.io.*;
public class Main{
public static void main(String args[]){
Scanner sc =new Scanner(System.in);
System.out.println("Enter the shipment code :");
int code = sc.nextInt();
String.format("%03d", code);
// fill the code
if( code == 111 ){
System.out.println("All ways");
}
else if( code == 110){
System.out.println("Airway and Waterway");
}
else if( code == 011){
System.out.println("Waterway and Roadway");
}
else if( code == 010){
System.out.println("Waterway");
}
else if( code == 101){
System.out.println("Airway and Roadway");
}
else if(code == 001){
System.out.println("Roadway");
}
}
}
You're doing something wrong here. 您在这里做错了。
011
, 010
, 001
are octal numbers, as they start with a zero. 011
, 010
, 001
是八进制数,因为他们开始以零。
Also, using String.format
is pointless here, as the resulting value is not used. 另外,在这里使用String.format
是没有意义的,因为不使用结果值。
This might be why your if
branches aren't taken into consideration. 这可能就是为什么不考虑您的if
分支的原因。
final String formattedValue = String.format("%03d", code);
Now you can use formattedValue
as a comparison value for your if
statements. 现在,您可以将formattedValue
用作if
语句的比较值。
Example 例
if ("111".equals(formattedValue)) { ... }
Note that maybe transforming the int
into a String
isn't necessary. 请注意,可能不需要将int
转换为String
。 But in case you insist on doing so, a good practice is to use a constant String
as the operand which calls equals(...)
. 但是,如果您坚持要这样做,一个好的做法是使用常量String
作为调用equals(...)
的操作数。
You're discarding the formatted value. 您正在舍弃格式化的值。 You need to store it in a variable and compare it as string: 您需要将其存储在变量中,并将其与字符串进行比较:
String formatted = String.format("%03d", code);
if( formatted.equals("111") ){
System.out.println("All ways");
}
// ...
Well, String.format("%03d", code)
, returns a string , and you are comparing to integers (octal integers, as LppEdd pointed out). 好吧, String.format("%03d", code)
返回一个string ,并且您正在与整数 (如LppEdd所指出的八进制整数)进行比较。
You should store the formatted string to a variable, eg 您应该将格式化的字符串存储到变量中,例如
String formatted = String.format("%03d", code);
and then compare it to Strings in your if/else statements, like so: 然后将其与if / else语句中的Strings进行比较,如下所示:
if(formatted.equals("011")) {...}
Don't format and remove any leading 0's in the condition and use switch 不要格式化和删除条件中的任何前导0并使用开关
int code = sc.nextInt();
// fill the code
switch(code) {
case 111:
System.out.println("All ways");
break;
case 110:
System.out.println("Airway and Waterway");
break;
case 11:
System.out.println("Waterway and Roadway");
break;
case 10:
System.out.println("Waterway");
break;
case 101:
System.out.println("Airway and Roadway");
break;
case 1:
System.out.println("Roadway");
break;
default:
System.out.println("Unknown code " + code);
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.