匿名使用者
匿名使用者 發問時間: 電腦與網際網路程式設計 · 5 年前

幫C語言轉java (20點)

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#include <unistd.h>

int function(int);

int deposit(int);

int withdraw(int);

int total(int);

int postoffice(void);

int passwd(void);

int last(int);

void mistake(void);

void check(void);

int main(void)

{

int input,count=0,passwd;

printf("華東郵局\n");

if(access("密碼.txt",F_OK) == 0)

{

FILE *fptr;

fptr=fopen("密碼.txt","r");

fscanf(fptr,"%d",&passwd);

fclose(fptr);

while(1)

{

printf("請輸入密碼\n");

scanf("%d",&input);

count++;

if(input==passwd)

{

printf("密碼輸入正確,\n");

postoffice();

break;

}

if(count>=3)

{

printf("密碼輸入錯誤超過三次\n");

mistake();

break;

}

}

}

else

{

printf("歡迎初次使用本系統\n");

printf("請先設定密碼(請牢記密碼)\n");

scanf("%d",&passwd);

FILE *pFile;

pFile = fopen("密碼.txt","w" );

fprintf(pFile,"%d",passwd);

f

1 個解答

評分
  • 5 年前
    最佳解答

    import java.io.BufferedReader;

    import java.io.FileNotFoundException;

    import java.io.FileReader;

    import java.io.FileWriter;

    import java.io.IOException;

    import java.util.Scanner;

    public class sn {

     public static void main(String[] args)

     {

      String filePath = "pwd.txt";

      String passWord = null;

      boolean isExistPassword = false;

      System.out.println("華東郵局");

      try

      {

       FileReader reader = new FileReader(filePath);

       BufferedReader bReader = new BufferedReader(reader);

       passWord = bReader.readLine();

       reader.close();

       if (passWord != null && passWord.isEmpty() == false)

       {

        isExistPassword = true;

       }

      }

      catch (FileNotFoundException e)

      {

       

      }

      catch (IOException e)

      {

       

      }

      if (isExistPassword == false)

      {

       System.out.println("歡迎初次使用本系統");

       System.out.println("請先設定密碼(請牢記密碼");

       passWord = (new Scanner(System.in)).nextLine();

       try

       {

        FileWriter writer = new FileWriter(filePath);

        writer.write(passWord);

        writer.close();

       }

       catch (IOException e)

       {

        

       }

      }

      else

      {

       int count = 0;

       while (true)

       {

        System.out.println("請輸入密碼");

        String input = (new Scanner(System.in)).nextLine();

        count ++;

        

        if (input.equals(passWord))

        {

         System.out.println("密碼輸入正確");

         postoffice();

         break;

        }

        else

        {

         System.out.println("密碼輸入錯誤");

         if(count>=3)

         {

          System.out.println("密碼輸入錯誤已達三次");

          mistake();

          break;

         }

        }

       }

      }

     }

     public static void postoffice() {}

     public static void mistake() {}

    }

還有問題?馬上發問,尋求解答。