这个程序核心是Scanner类的使用
package com.test;
import java.util.Scanner;
public class Test {
public static void main(String[] args)throws Exception{
Scanner s= new Scanner(System.in);
System.out.print("输入第一个数:");
float a=s.nextFloat();
System.out.print("输入第二个数:");
float b=s.nextFloat();
System.out.println("两个数的和是:"+(a+b));
}
}
运行效果如下
Java输入两个数,输出和:
import java.util.*;
public class Size{
public static void main(String[] args)throws Exception{
Scanner s= new Scanner(System.in);
System.out.print("请输入第一个数:");
float a=s.nextFloat();
System.out.print("请输入第二个数:");
float b=s.nextFloat();
System.out.println("两个数的和是:"+(a+b));
System.out.println("两个数的差是:"+(a-b));
System.out.println("两个数的积是:"+(a*b));
System.out.println("两个数的商是:"+(a/b));
}
}
public static void main(String[] args) {
System.out.print("请输入两个数,以空格分开:");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a+b);
}
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
System.out.println(a+b);
}