2-2-2 判断语句-if-else新例子?

来源:互联网 发布:dev c 如何创建c语言 编辑:程序博客网 时间:2024/06/06 11:45

例子一

老师举例:

if( height < MAX ){growth = 10;}else{growth = MAX - weight;}


自己补充成:

public class Hello {public static void main(String[] args) {// TODO Auto-generated method stubfinal int MAX = 66;Scanner in = new Scanner(System.in);int height = in.nextInt();int weight = in.nextInt();int growth;if( height < MAX ){growth = 10;}else{growth = MAX - weight;}System.out.println(growth);}}
无报错,无运行

final int MAX = 66;
设max=66


例子二 输小时,求工资+加班费

public class Hello {public static void main(String[] args) {// TODO Auto-generated method stubfinal double RATE = 8.25;final int STANDARD = 40;double pay = 0.0;Scanner in = new Scanner(System.in);System.out.print("Enter the number of hours worked:");int hours = in.nextInt();System.out.println();if( hours < STANDARD ){pay = STANDARD * RATE + (hours-STANDARD) * (RATE* 1.5);}else{pay = hours + RATE;}System.out.println("Gross earnings:" + pay);}}


例子三 

<span style="font-size:14px;">public class Hello {public static void main(String[] args) {// TODO Auto-generated method stubfinal double PASS = 8.25;Scanner in = new Scanner(System.in);System.out.print("请输入成绩:");int score = scan.nextInt();System.out.println("你输入的成绩是"+score);if( score < PASS ){System.out.println("很遗憾,唔及格");}else{System.out.println("恭喜,及格");}System.out.println("再见");}}</span>

 不知道是老师出错了还是我还没学到,而且程序也在这一行显示出错:

int score = scan.nextInt();
按上面的例子应该是奋斗

<pre name="code" class="java" style="font-size: 14px;">int score = in.nextInt();


例子延伸

如果后面改成,而输入<60:

else{System.out.println("恭喜,及格");System.out.println("再见");}

那么再见不会出现。

0 0