while循環題解

 //求1+2+3+...+100
class Demo01{
    public static void main(String[] args) {
        sum=0; //用於存放最後的結果
        i=1;  //從1開始
        while(i<=100){ //i小於等於100是否爲真
            sum+=i;   //爲真,加i
            i++;      //i加1 
        }
        System.out.println(sum); //不爲真,打印結果
    }
}
/*
猜電腦的隨機生成的數字
*/
import java.util.*;
class Demon02{
    public static void main(String[] args) {

        Scanner scanner=new Scanner(System.in);
        int com=new Random().nextInt(101);//[0,n)  //電腦隨機生成1-100之間一個數
       
        while(true){     //無條件開啓死循環,因爲沒有初始條件,也不知道什麼時候結束
            System.out.print("Guess a number:");   //提示用戶輸入一個數
            int num=scanner.nextInt();  //接受用戶輸入
            if(num>com){    //如果用戶猜測的數字大於隨機數,提示數字太大
                System.out.println("too high");  
            }else if (com==num){  //如果用戶猜測的數字等於隨機數,提示正確
                System.out.println("yes,you are right!");                
                break;
            }else{    //如果用戶猜測的數字小於隨機數,提示數字太小
                System.out.println("too low");
            }
        }
    }
}

/* 哨兵法  */
import java.util.Scanner;
class Demon03{
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.print("請輸入一個數字:"); 
        int number=scanner.nextInt();
        while(true){     //無條件先進入循環
            boolean flag=true;  //定義一個“哨兵”
            for(int i=2;i<number;i++){  //進入for循環
                if(number%i==0){  只要i是number的因子
                    System.out.print(i+",");  //打印i
                    number=number/i;  //將商給number繼續查找因子
                    flag=false; //將“哨兵”值改變
                    break;   //跳出當前循環
                }
            }
            if(flag){  //當循環時是正常結束,則不會改變flag的值,判斷值是否被改變
                System.out.print(number+"。");  //沒有改變打印最後一個因子
                break; //跳出死循環
            }
        }
    }
}

import java.util.*;
class Demon04{
    public static void main(String[] args) {
        String[] arr={"剪刀","石頭","布"};  //定義一個數組用來存放出現的類型
        Scanner scanner=new Scanner(System.in);
        Random random=new Random(); 
        int usrCount=0;  //用戶贏的次數
        int comCount=0;  //電腦贏的次數
        // 規定剪刀0 石頭1 布2
        while(true){  //先進入循環
            //用戶輸入一個
            System.out.print("請輸入 剪刀0 石頭1 布2:");
            int usr;
            try{
                usr=scanner.nextInt();  //IO流阻塞
            }catch(java.util.InputMismatchException e){
                System.out.println("輸入錯誤,請重新輸入");
                scanner=new Scanner(System.in);
                continue;    //跳到下一次循環
            }
            if(usr<0||usr>2){ //如果用戶沒有輸入剪刀、石頭、布任何一個
                System.out.println("輸入錯誤,請重新輸入");
                continue;    //跳到下一次循環
            }
            //電腦隨機生成一個
            int com=random.nextInt(3);  //[0,3)
            if(usr==com){    //平局
                System.out.println("玩家是"+arr[usr]+",電腦是"+arr[com]+",平局!");
            }else if(usr-com==1||usr-com==-2){    //玩家贏
                System.out.println("玩家是"+arr[usr]+",電腦是"+arr[com]+",玩家贏!");
                usrCount++;
            }else{   //電腦贏
                System.out.println("玩家是"+arr[usr]+",電腦是"+arr[com]+",電腦贏!");
                comCount++;
            }
            if(usrCount==2 || comCount==2){ //只要任何一方贏了兩次
                break; //死循環結束
            }
        }
        if(usrCount==2){  //打印最終結果
            System.out.println("最終,玩家贏!");
        }else{
            System.out.println("最終,電腦贏!");
        }
    }
}

import java.util.Scanner;
class Demon05{
    public static void main(String[] args){
        //輸入一個十進制整數
        Scanner scanner = new Scanner(System.in);
        System.out.print("請輸入一個數字:");
        int number=scanner.nextInt();
      
        String str=""; //存放計算的結果
        while(true){   //無條件進入循環
            int rest=number%16; //先將輸入的數模16
            if(rest<10){  //餘數小於10
                str=rest+str; //直接將餘數加結果,將第一的計算的值放在第一位
            }else{ 
                str=(char)(55+rest)+str;  //餘數大於10,將對應的ASCII碼轉化爲字符,不懂的可以去看一下ASCII碼錶
            }
            number=number/16; //將number變成模16的商
            if(number==0){ //此時商爲0
                break;  //數字已轉化完,跳出死循環
            }
        }
        System.out.println(str);

    }
}

import java.util.Scanner;
class Demon06{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter number:");       
        int max=0; //保存輸入最大的數
        int count=0;  //保存最大數被輸入了次數
        while(true){ // 先進入循環
            int num=scanner.nextInt(); //接受輸入
            if(num==0){  //接受的輸入爲0
                break;  //結束死循環
            } 
            if(num>max){   //輸入的數字不爲0,且大於當前最大的數
                max=num;   //保存最大數
                count=1;    //計數器加1
            }else if(num==max){   //如果與當前數最大數相等
                count++;   //計數器加1
            }
        }
        System.out.println(max+"的次數爲"+count);  //最後打印結果
    }
}

 

發佈了34 篇原創文章 · 獲贊 24 · 訪問量 1936
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章