publicclassBlockStudy{ publicstaticvoidmain(String[] args){ int n; { int k; /*k is only defined up to here*/ int n; /*ERROR: n can't redefine n in inner block*/ } } }
publicclassInterruptStudy{ publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); System.out.println("How much money do you need to retire?"); double goal = in.nextDouble(); System.out.println("How much money will you contribute every year"); double payment = in.nextDouble(); System.out.println("Interest rate in %:"); double interestRate = in.nextDouble(); double balance = 0; int years = 0; while(years<=100) { //while(years<=100 && balance < goal) { balance+=payment; double interest = balance*interestRate/100; balance +=interest; if(balance>=goal) break; /*if(balance < goal) 不使用break需要检测两次balance < goal*/ years++; } System.out.println("You need "+years+" years to save a targeted amount of retirement money."); } }
publicclassBreakLabelStudy{ publicstaticvoidmain(String[] args){ Scanner in= new Scanner(System.in); int t; int n=0; read_data: while(n<=10) { for(int i=1;i<=5;i++) { System.out.print("Please enter a number>=0:"); t = in.nextInt(); if(t<=0) { break read_data; } } n++; } if(n>10){ //carry out normal processing } else{ //deal with bad situation System.out.print("You input a bad number."); } } }
publicclassBreakLabelStudy{ publicstaticvoidmain(String[] args){ Scanner in= new Scanner(System.in); int t; int n=0; read_data: { System.out.print("Please enter a number>=0:"); t = in.nextInt(); if(t<=0) { break read_data; } System.out.println("You must input a good num."); n=1; } // jump here when the break statement executes if(n==0) { System.out.print("You input a bad number."); } } }
publicclassBreakLabelStudy{ publicstaticvoidmain(String[] args){ Scanner in= new Scanner(System.in); int sum=in.nextInt(); int goal=in.nextInt(); while (sum<goal) { System.out.print("Enter a number:"); int n=in.nextInt(); if(n<0) continue; sum+=n; //not executed i System.out.println(sum); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package com.noob.base;
import java.util.Scanner;
publicclassBreakLabelStudy{ publicstaticvoidmain(String[] args){ Scanner in= new Scanner(System.in); int sum=0; for(int count=1;count<100;count++) { System.out.println("Enter a number:"); int n=in.nextInt(); if(n<0) continue; /*如果n<0,则continue语句跳到count++语句*/ sum+=n; } } }
publicclassBigNumber{ publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); BigInteger lotteryOdds = BigInteger.valueOf(1); for(int i=1;i<=a;i++){ lotteryOdds=lotteryOdds.multiply(BigInteger.valueOf(b-i+1)).divide(BigInteger.valueOf(i)); } System.out.println("You odds are 1 in "+lotteryOdds); } }
publicclassArrayStudy{ publicstaticvoidmain(String[] args){ /* 最后一个值后面允许有逗号,方便不断为数组增加值 */ String[] authors= { "James Gosling", "Bill Joy", "Guy Steele", //add more names here and put a comma after each name }; /*声明匿名变量*/ System.out.println(newint[] {17,19,23,9,37}[1]); /* * anonymous int array : new int[] { 1, 2, 3, 4}; * anonymous String array : new String[] {"one", "two", "three"}; * anonymous char array : new char[] {'a', 'b', 'c'); */ /* Best use of anonymous array is to implement variable argument method which can be invoked with a different number on arguments. */ sum(newint[]{ 1, 2, 3 }); } publicstaticvoidsum(int[] a) { int total = 0; // using for-each loop for (int i : a) total = total + i; System.out.println("The sum is:" + total); } }
1 2 3 4 5 6 7 8 9
publicclassArrayStudy{ publicstaticvoidmain(String[] args){ /*在Java中允许有长度为0的数组,如结果为数组的方法,结果为空时就需要长度为0的数组*/ int[] a =newint[0]; int[] b =newint[]{}; /*长度为0的数组与null并不相同*/ System.out.println(a.length); } }
publicclassLotteryDrawing{ publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); System.out.println("How many numbers do you need to draw?"); int k = in.nextInt(); System.out.println("What is the highest number you can draw?"); int n = in.nextInt(); int[] numbers= newint[n]; for(int i=0;i<numbers.length;i++) { numbers[i] = i+1; } int[] result = newint[k]; for(int i=0;i<result.length;i++) { /*Math.random方法将返回一个0到1之间(包含0、不包含1)的随机浮点数*/ int r=(int) (Math.random()*n); result[i]=numbers[r]; numbers[r]=numbers[n-1]; /* 确保不会再次抽取到那个数值,因为所有抽彩的数值必须不相同。 因此,这里用数组中的最后一个数值改写number[r],并将n减1 */ n--; } Arrays.sort(result); System.out.println("Bet the following combination.It'll make you rich!"); for(int r:result) { System.out.println(r); } } }