顯示具有 第六章 標籤的文章。 顯示所有文章
顯示具有 第六章 標籤的文章。 顯示所有文章

2017年6月20日 星期二

第六章 中獎查詢

題目:預先設定六個介於1~49之間中獎號碼,並讓使用者輸入六個1~49之間的號碼。如果兩者相同就顯示中獎的訊息


import java.util.*;
public class HomeWork0608 {
public static void main(String[] args){
Scanner sc=new
Scanner(System.in);

int []envoice={3,5,18,28,33,40};//事先排好中獎號碼
int []check=new int[6];

for(int j=0;j<6;j++){
check[j]=sc.nextInt();
}

for (int i=0;i<check.length-1;i++){
for (int j = 0; j < check.length-1-i; j++){
if(check[j]>check[j+1]){
check[j]=check[j]^check[j+1];
check[j+1]=check[j]^check[j+1];
check[j]=check[j]^check[j+1];
}//使用XOR交換,未使用到變數儲存變數
}
}
//輸入內容,並以氣泡法排序

for(int t=0;t<6;t++){
if(check[t]!=envoice[t]){
System.out.print("未中獎");
break;
}
else
System.out.print("恭喜中獎");
break;
}//開始查詢是否中獎


sc.close();


}


}

2017年6月19日 星期一

第六章 數學方程式測試

題目:讓使用者輸入x&n,計算出上列的式子


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class HomeWork0610 {

public static void main(String[] args)throws IOException{
BufferedReader br= new
BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入x ");

double x=java.lang.Double.parseDouble(br.readLine());

System.out.println("請輸入n ");

double n=java.lang.Double.parseDouble(br.readLine());

double sum=0;

for(int i=1;i<=n;i++)
sum+=((x+i)/(n-i+1));
if(n==0)
System.out.print("n不可為零");
else
System.out.print(sum);
}

}

第六章 密碼測試

題目:
讓使用者輸入兩次密碼(四位數字)
給予兩次密碼輸入錯誤的機會
第三次輸入錯誤就給予錯誤的訊息

本人有使用類似作業系統的概念
Monitor? 號誌?

import java.util.Scanner;
public class HomeWork0609 {

public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
     System.out.println("請輸入四位數密碼");

int key=1234;//正確密碼
boolean a=false;//測試關卡用
int count=0;//錯誤次數

while(a!=true&(3-count)>0){
int s = sc.nextInt();
if(s==key)
{
System.out.print("輸入正確");
a=true;
}

else
{
if((3-count)>1){
System.out.print("輸入錯誤,請重新輸入");
System.out.println("還可再試"+(2-count)+"次");
count++;
}
else
{
System.out.print("錯誤超過三次,系統中止");
count++;
}
}
}
sc.close();
}
}

第六章 菱形星星

public class HomeWork0607 {

public static void main(String[] args) {

for(int i=1;i<=4;i++){

for(int j=3;j-i>=0;j--){
System.out.print(" ");
}//左上半部空格

for(int k=1;k<=2*i-1;k++)
{
System.out.print("*");
}//中上半部的星號

System.out.println("");
}

for(int i=1;i<4;i++){

for(int j=1;j<i+1;j++)
{
System.out.print(" ");
}//印左下半部空白處

for(int k=7;k-i*2>0;k--){
System.out.print("*");
}//印左下半部的星號

System.out.println("");
}
}
}

第六章 上三角形

public class HomeWork06071 {

public static void main(String[] args){
for(int i=0;i<5;i++){
for(int j=1; j-i<2;j++){

System.out.print("*");

}
System.out.print("\n");
}


}