package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out .println(“你好”);
Scanner in =new Scanner(System.in);
//System.out.println(“echo:”+in.nextLine());
int price;
price=in.nextInt();(注释:此处的=,是一个动作,而不是表达关系,而是把右边的值赋给左边。左边是接收值的)
System.out.println(“100-“+price+”=”+(100-price));
}
}
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out .println(“你好”);
Scanner in =new Scanner(System.in);
//System.out.println(“echo:”+in.nextLine());
int price=0;(注释:表示初始化)
price=in.nextInt();(注释:此处的=,是一个动作,而不是表达关系,而是把右边的值赋给左边。左边是接收值的)
System.out.println(“100-“+price+”=”+(100-price));
}
}
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out .println(“你好”);
Scanner in =new Scanner(System.in);
//System.out.println(“echo:”+in.nextLine());
int price;
System.out.println(price);
(注释:输入上一行后,句子前面就会出现叉叉和灯泡,叉叉提示:The local variable price may not have been initialized(本地变量可能还没有被初始化) 点击灯泡,出现提示,双击它,就会给前一行赋值0。)
price=in.nextInt();
System.out.println(“100-“+price+”=”+(100-price));
}
}
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out .println(“你好”);
Scanner in =new Scanner(System.in);
//System.out.println(“echo:”+in.nextLine());
int price=0;
System.out.println(price);(注释:句子前面就会出现叉叉和灯泡,提示:The local variable price may not have been initialized点击灯泡,双击灯泡提示行,就会给int price赋值0。灯泡表示它有一些想法可以帮助我们改进。)
price=in.nextInt();
System.out.println(“100-“+price+”=”+(100-price));
}
}
如果在一行当中,定义多个变量的话,每个变量都要一个单独的值。
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out .println(“你好”);
Scanner in =new Scanner(System.in);
//System.out.println(“echo:”+in.nextLine());
int price = 0,amount=0;(注:最好是一行定义一个变量,以便今后对代码进行定义和维护)
System.out.println(price);
price=in.nextInt();(注——表达式)
System.out.println(“100-“+price+”=”+(100-price));
}
}
感叹号,表示这个句子可能有问题。这个问题不会影响程序的运行。这叫警告。如,这个变量没有被用到。
Exception(注:异常) in thread “main” java.util.InputMismatchException
如果希望用变量来表达100:
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out .println(“你好”);
Scanner in =new Scanner(System.in);
//System.out.println(“echo:”+in.nextLine());
int price = 0,amount=100;
System.out.println(price);
price=in.nextInt();
System.out.println(amount+”-“+price+”=”+(amount-price));
}
}
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out .println(“你好”);
Scanner in =new Scanner(System.in);
//System.out.println(“echo:”+in.nextLine());
final int amount=100;(注:amount是变量 ,在这个程序中是不变的,是常量,final 表示常量,amount不能再被赋值。)
int price = 0;
System.out.println(price);
price=in.nextInt();
System.out.println(amount+”-“+price+”=”+(amount-price));
}
}
如果希望amount不是固定的,是用户输入的。
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out .println(“你好”);
Scanner in =new Scanner(System.in);
//System.out.println(“echo:”+in.nextLine());
int amount=100;
int price = 0;
amount=in.nextInt();
System.out.println(price);
price=in.nextInt();
System.out.println(amount+”-“+price+”=”+(amount-price));
}
}
假如希望输入输出友好一点:
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out .println(“你好”);
Scanner in =new Scanner(System.in);
//System.out.println(“echo:”+in.nextLine());
int amount=100;
int price = 0;
System.out.print(“请输入票面”);
amount=in.nextInt();
System.out.print(“请输入金额”);
System.out.println(price);
price=in.nextInt();
System.out.println(amount+”-“+price+”=”+(amount-price));
}
}