用Java编写程序 任意两个输入两个整数 输出它们相加的结果
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 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 class Add{
public static void mian(String[] args) {
System.out.println("请输入被加数a:");
Scanner scanner1 = new Scanner(System.in);int num1 = scanner1.nextInt();
System.out.println("请输入被银手旅加数b");Scanner scanner2 = new Scanner(System.in);
薯册 int num2 = scanner2.nextInt();
锋凳 int sum = add(num1,num2);System.out.println("a + b = " + sum);
}
static int add(int a, int b) {
return a+b;
}
}
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<charMaxLength.length;i++){
c1=charMaxLength[i]-'0';
if(i<charMinLength.length){
c2=charMinLength[i]-'0';
}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()方法,该方法将字符串转化成字符数组,获取到数组后就可以利用其进行修改,