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
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
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
userList.stream().collect(Collectors.groupingBy(User::getAddress));
System.out.println(addressMap);
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("输入错误,请输入两位数!");
}
else if(b>100||b<10){
System.out.println("输入错误,请输入两位数!");
}
else
System.out.println(a+b);
}
}
注意你的几处错误,||才是或的意思。
import java.util.Scanner;
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 basic.aSelf;
import java.util.Scanner;
public class AnyNOPlus {
/**
* @param args
*/
public static void main(String[] args) {
// System.out.println(invertStr("12345"));
Scanner s=new Scanner(System.in);
String str1=s.nextLine();
String str2=s.nextLine();
System.out.println(AnyNOPlusDemo(str1,str2));
}
/**
* 实现任意两个整数相加
* @param str1
* @param str2
* @return
*/
public static String AnyNOPlusDemo(String str1,String str2){
str1=invertStr(str1);//4321
str2=invertStr(str2);//1
//判断谁的长度长
char[] charMaxLength;
char[] charMinLength;
if(str1.length()>=str2.length()){
charMaxLength=str1.toCharArray();
charMinLength=str2.toCharArray();
}else{
charMaxLength=str2.toCharArray();
charMinLength=str1.toCharArray();
}
//进位
int other=0;
//返回的String
String strAll="";
//相加
int c=0;
int c1=0;
int c2=0;
for(int i=0;i
if(i
}else{
c2=0;
}
c=(other+c1+c2)%10;
//判断进位
if(other+c1+c2>=10){
other=1;
}else{
other =0;
}
//相加
strAll+=c;
}
return invertStr(strAll);
}
/**
* 将字符串倒转
* @param str
* @return
*/
public static String invertStr(String str){
String result="";
for(int i=str.length()-1;i>=0;i--){
char c=str.charAt(i);
result+=c;
}
return result;
}
}
完整代码如下:
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()方法,该方法将字符串转化成字符数组,获取到数组后就可以利用其进行修改,