import java.util.Scanner;
public class java4 {
public static void main(String[] args) {
Scanner i=new Scanner(System.in);
int a=i.nextInt();
int b=i.nextInt();
if(a>100&&a<10);
{System.out.println("输入错误,请输入两位数!");
};
if(b>100&&b<10);
{System.out.println("输入错误,请输入两位数!");}
{System.out.println(a+b);}
}
}
帮忙改下吧,谢谢啦!
import java.util.Scanner;
public class Program1 {
public static void main(String[] args) {
Scanner i = new Scanner(System.in);
int a = i.nextInt();
int b = i.nextInt();
if (a > 100 || a < 10 || b>100 ||b<10) {
System.out.println("输入错误,请输入两位数!");
}else{
System.out.println(a + b);
}
}
}
java的用法小结
一、简单的stream,parallelStream慎用
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
list.parallelStream().forEach(System.out::println);
list.parallelStream().forEachOrdered(System.out::println);
二、简单的stream, collect, sum, count,
1、取某一字段 stream & collect
System.out.println(userList.stream().map(User::getAddress).collect(Collectors.toList()));
System.out.println("---------");
ArrayList<String> arrayList =
userList.stream().map(User::getAddress).collect(Collectors.toCollection(ArrayList::new));
System.out.println(arrayList);
System.out.println("---------");
System.out.println(userList.stream().map(User::getAddress).collect(Collectors.joining("@@")));
2、取总和 sum
System.out.println(userList.stream().mapToInt(User::getAge).sum());
3、分组
Map<String, List<User>> addressMap =
userList.stream().collect(Collectors.groupingBy(User::getAddress));
System.out.println(addressMap);
public static void mian(String[] args) {
System.out.println("请输入被加数a:");
Scanner scanner1 = new Scanner(System.in);int num1 = scanner1.nextInt();
System.out.println("请输入被加数b");int num2 = scanner2.nextInt();
int sum = add(num1,num2);完整代码如下:
package testcode;
import java.util.Scanner;
public class PAT1001 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
int a=Integer.parseInt(scanner.next());
int b=Integer.parseInt(scanner.next());
Integer c=a+b;
String s = c.toString();
char[] strings2 = s.toCharArray();
int num;
char[] output;
int i=0,k;
if (strings2[0]=='-') {
if ((strings2.length-1)%3==0) {
num=(strings2.length-4)/3;
}else {
num=(strings2.length-1)/3;
}
output=new char[strings2.length+num];
i=0;
k=output.length;
for (int j = strings2.length-1; j >=0 ; j--) {
i++;
k--;
if (i%3==0&&k>=2) {
output[k]=strings2[j];
k--;
if(k>=0){
output[k]=',';
}
}else{
output[k]=strings2[j];
}
}
}else{
if (strings2.length%3==0) {
num=(strings2.length-3)/3;
}else {
num=strings2.length/3;
}
output=new char[strings2.length+num];
i=0;
k=output.length;
for (int j = strings2.length-1; j >= 0; j--) {
i++;
k--;
output[k]=strings2[j];
if(i%3==0){
k--;
if(k>=0){
output[k]=',';
}
}
}
}
System.out.println(output);
}
}
拓展:程序目的是将形如-23455066,2345539转化成-23,455,066,2,345,539形式。用到的主要方法是String的toCharArray()方法,该方法将字符串转化成字符数组,获取到数组后就可以利用其进行修改,
本回答被网友采纳