我正在尝试转换字符串:
let time = "7:30"
Run Code Online (Sandbox Code Playgroud)
整数:
let hour : Int = 7
let minutes : Int = 30
Run Code Online (Sandbox Code Playgroud)
我目前正在遍历字符串:
for char in time.characters {
}
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何将char转换为int.任何帮助将不胜感激.
我正在编写一个代码,它将输入输入的数字,只添加偶数位置的值.
例如:
如果用户输入53429,则偶数位置的总和为5.
我的问题是我试图将偶数位置的字符串转换回整数并将它们加在一起.这就是我所拥有的
当我尝试将字符串解析为整数时,我一直收到错误.
Cannot find symbol
symbol : method parseInt(java.lang.String)
location: class Integer
码:
import java.util.Scanner;
public class NumberSums {
public static void main(String [] args) {
Scanner keyboard=new Scanner(System.in);
System.out.print("Enter a number: ");
String x=keyboard.next();
String s1 = x;
int length = s1.length();
if (length<5) {
System.out.println("Invalid value");
System.exit(0);
}
if (length>5) {
System.out.println("Invalid value");
System.exit(0);
}
String s2 = s1.substring(0,1);
String s3 = s1.substring(1,2);
String s4 = s1.substring(2,3);
String s5 = s1.substring(3,4);
String s6 = s1.substring(4,5); …Run Code Online (Sandbox Code Playgroud) 程序告诉用户输入的整数是零,正,偶数还是奇数,还是负数,偶数或奇数.
我的问题是如果输入非整数,我想在println中添加错误.看最后一行.
import java.util.Scanner;
public class IntegerCheck {
public static void main(String [] args) {
int x;
System.out.println("Enter an integer value:");
Scanner in = new Scanner(System.in);
x = in.nextInt();
//String x = in.nextInt();
if (((x % 2) == 0) && (x< 0))
System.out.println(x + " is a negative, even integer.");
else if (((x % 2) == 0) && (x == 0))
System.out.println(x + " is Zero.");
else if ((x % 2)==0)
System.out.println(x + " is a positive, even integer.");
if …Run Code Online (Sandbox Code Playgroud)