題目:
試寫一程式,讓使用者可輸入兩次密碼(四位整數),並驗證輸入的密碼是否符合,輸入三次不正確即顯示錯誤訊息
小弟的程式寫法(作業系統:W7 編譯器:JCreator LE5.00)
如下:
import java.io.*;
public class test82 {
public test82() {
}
public static void main(String[] args)
throws IOException {
BufferedReader a = new
BufferedReader(new InputStreamReader(System.in));
BufferedReader b = new
BufferedReader(new InputStreamReader(System.in));
int count=0;
while (true) {
if(count==3){
System.out.println("錯誤3次!!!");
break;
}
System.out.println("請輸入第一道密碼:");
System.out.print(">");
String ar = a.readLine();
int i = Integer.parseInt(ar);
if(i==9527){
System.out.println("請輸入第二道密碼:");
System.out.print(">");
String br = b.readLine();
int j = Integer.parseInt(br);
if(j==1688) {
System.out.println("兩道密碼正確!");
break;
}
}
count++;
}
}
}
______________________________________________
實際執行結果如下:
----------------------
請輸入第一道密碼:
>0
請輸入第一道密碼:
>0
請輸入第一道密碼:
>0
錯誤3次!!!
Process completed.
----------------------
請輸入第一道密碼:
>9527
請輸入第二道密碼:
>1688
兩道密碼正確!
Process completed.
----------------------
請輸入第一道密碼:
>9527
請輸入第二道密碼:
>0
請輸入第一道密碼:
>0
請輸入第一道密碼:
>0
錯誤3次!!!
Process completed.
----------------------
小弟不才.是本科系新生.沒有"任何"程式語言基礎.....想利用寒假自學趕一下進度
以上的問題是第3次執行時(第一道密碼正確.第二道錯誤跳回第一道重新輸入)
希望可以改成"第二道錯誤跳回第二道重新輸入"不用在重新輸入第一道
但..不論我怎麼寫都寫不出來.....
小弟現在自學的進度是讀到條件分支.迴圈.陣列(使用者輸入的介面我是看書上照打並稍微了解一下而已)
我寫的程式可能看起來很好笑(總覺得落落長...看起來怪怪的..)...
但是我想了很久才寫出來的><
有去爬過文....
但...絕大部分...用的程式碼對我現在來講都太艱澀難懂...
且都是直接貼出答案...
我又不是要趕作業...要答案有屁用.....
請高手不吝教導^^
但小弟編譯時會產生錯誤
--------------------Configuration: <Default>--------------------
C:\Users\Leon\Documents\Java\Ch6迴圈\test82.java:36: illegal start of expression
if(i<>9527){
^
1 error
----------------------------------------------------------------------
是我修改的地方不對嗎??
import java.io.*;
public class test82 {
public test82() {
}
public static void main(String[] args)
throws IOException {
BufferedReader a = new
BufferedReader(new InputStreamReader(System.in));
BufferedReader b = new
BufferedReader(new InputStreamReader(System.in));
int count=0;
while (true) {
if(count==3){
System.out.println("錯誤3次!!!");
break;
}
System.out.println("請輸入第一道密碼:");
System.out.print(">");
String ar = a.readLine();
int i = Integer.parseInt(ar);
if(i<>9527){ //修改這邊
System.out.println("請輸入第二道密碼:");
System.out.print(">");
String br = b.readLine();
int j = Integer.parseInt(br);
if(j==1688) {
System.out.println("兩道密碼正確!");
break;
}
}
count++;
}
}
}
這樣你可以用if 來寫.
int firstPasswordVerified=0;
//0=not matched, 1=matched. 檢查第一個密碼,對的話把這個flag設成1. 其實你可以用bool不用int.只是我懶.
int secondPasswordVerified=0;
int incorrectPasswordCounter=0;
while(incorrectPasswordCounter<3)
{
//當你已經有第一個密碼是懟的時候
if (firstPasswordVerified ==1)
{
//檢查第2個密碼
if(secondPasswordVerified==1)
{
//你通過密碼測試
}
}
}
是在迴圈上加上標籤和一個switch
作法如下:
oneloop:while (true) {
if(count==3){
System.out.println("錯誤3次!!!");
break;
}
System.out.println("請輸入第一道密碼:");
System.out.print(">");
String ar = a.readLine();
int i = Integer.parseInt(ar);
if(i==9527){
System.out.println("請輸入第二道密碼:");
System.out.print(">");
String br = b.readLine();
int j = Integer.parseInt(br);
switch (x){
case 1688:
System.out.println("兩道密碼正確!");
break;
default:
continue oneloop;
我的想法是用switch抓第二次錯誤
然後利用continue跳掉while迴圈 執行if(i==9527)這個判斷式
但執行結果卻和原本一樣?!
請教一下為什麼??
我剛剛上傳了passwords.class的zip檔案,解壓縮後你放在C:\
打java passwords
按下enter
附加壓縮檔: 201101/mobile01-6da96355355fc2b5a0647e52e17614a8.zip
這是不是你想要的結果?
public class test82
{
public static void main(String[] args)throws IOException
{
BufferedReader a = new
BufferedReader(new InputStreamReader(System.in));
BufferedReader b = new
BufferedReader(new InputStreamReader(System.in));
int count=0;
boolean password_one=false;//這個就是flag,也就是當作一個標記
while (true)
{
if(count==3)
{
System.out.println("錯誤3次!!!");
break;
}
if(!password_one)
{
System.out.println("請輸入第一道密碼:");
System.out.print(">");
String ar = a.readLine();
int i = Integer.parseInt(ar);
if(i==9527)
password_one=true;
else
{
count++;
continue;
}
}
if(password_one)
{
System.out.println("請輸入第二道密碼:");
System.out.print(">");
String br = b.readLine();
int j = Integer.parseInt(br);
if(j==1688)
{
System.out.println("兩道密碼正確!");
break;
}
}
count++;
}
}
}
------------------------
constructor在宣告初始畫的時候沒有要做事情的話
其實可以不用寫
java會預設一個constructor給他
試試看這個吧XD
我也只是java的初階使用者XD
內文搜尋

X