좋아요. 그래서 저는 기본적인 자바 프로그래밍이있는 CS 보안 클래스를 사용하고 있습니다. 우리의 첫번째 과제는 BigInteger
입니다. 그러나 우리는 또한 우리 프로그램을 "총알 증명"해야합니다. 제 방법이 가장 이상적이지는 않지만, 첫 번째 입력을 제외하고는 작동합니다. 일명 input.new---();
중 하나에 대해 잘못된 입력을 입력하면 내 프로그램에서 유효한 숫자로 다시 시도하라는 메시지가 표시됩니다. 하지만 첫 번째 input.nextInt();
은 여전히 유효하지 않은 입력 및 충돌을 받아 "java.util.InputMismatchException"
을 표시 한 다음 스캐너에 관한 정보를 제공합니다. 왜 이런 일이 일어나고 있는지에 대한 아이디어가 있습니까? p.s. 어떤 상황에서도 프로그램에서 오류 로그를 표시하면 안됩니다.스캐너로 입력 불일치 예외를 잡아/시도하지 않습니까?
import java.io.*;
import java.util.*;
import java.math.*;
class BigNumber{
public static void main(String args[]){
while(true){
try{
Scanner input = new Scanner(System.in);
System.out.print("if you wish to exit the program type in '0,' to continue running the program type in any other value: ");
double esc= input.nextDouble();
if(esc == 0){ break;}
else{System.out.println("Ok, program running...");}
input.nextLine();
System.out.print("Enter number a: ");
String aValue = input.nextLine();
System.out.print("Enter number b: ");
String bValue = input.nextLine();
System.out.print("Enter a number 'n': ");
String nValue = input.nextLine();
while (Integer.parseInt(nValue) < 1)
{
if (Integer.parseInt(nValue) < 1)
{
System.out.print("Please enter a valid number (n>1): ");
nValue = input.nextLine();
}
}
BigInteger a= new BigInteger(aValue);
BigInteger b= new BigInteger(bValue);
BigInteger n= new BigInteger(nValue);
System.out.println("---------------------------");
System.out.println("1) a xor b: " + a.xor(b));
System.out.println("2) b xor b: " + b.xor(b));
System.out.println("3) a xor b xor a: " + a.xor(b).xor(a));
System.out.println(" ");
System.out.println("4) ab mod n: " + a.modPow(b,n));
System.out.println(" ");
System.out.println("5) a shifted to the right by 6: " + a.shiftRight(6));
System.out.println("6) b shifted to the left by 3: " + b.shiftLeft(3));
System.out.println("---------------------------");
}
catch (NumberFormatException e){
System.out.println("-----> Please try entering a valid number(s) <-----");
}
}
}
}