Witam, to bedzie moj pierwszy post , dopiero zaczynam nauke javy wiec spotakalem sie z bledami w kodzie prostego algorytmu, musze znalezc bledy:
public static void main(String[] args) {
int firstValue = 0;
secondValue;
String operator = "";
int proposedResult;
int realResult
// depending on the number of provided arguments use appropriate case block
switch(args.length) {
case 4 :
operator = args[0];
firstValue= fromBinaryString(args[1]);
proposedResult = fromBinaryString(args[3]);
break;
case 5 :
firstValue = fromBinaryString(args[0]);
operator = args[1];
secondValue = fromBinaryString(args[2]);
proposedResult = fromBinaryString(args[4]);
break;
default :
System.err.println("Wrong argument count - only four or five arguments allowed!");
System.err.println("\tExamples:");
System.err.println("\t\t0101 AND 1111 = 0000");
System.err.println("\t\tNOT 1111 = 0000");
System.exit(0);
}
// count the real value
if (operator.equals("AND")) {
realResult = firstValue & secondValue;
} if else (operator.equals("NOT")) {
realResult = ~firstValue;
} else {
System.err.println("ERROR! Unknown operator: "+operator);
System.exit(0);
}
// display the results
if (proposedResult == realResult) {
System.out.println("CONGRATULATIONS! "+args[0]+" "+args[1]+" "+args[2]+" is "+toBinaryString(realResult));
} else {
System.out.println("BAD ANSWER! "+args[0]+" "+args[1]+" "+args[2]+" is "+toBinaryString(realResult)+" not "+proposedResult);
}
static int fromBinaryString(String s) {
int result = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '0') result = 2 * result;
else if (c == '1') result = 2 * result + 1;
}
return result;
}
static String toBinaryString(int n) {
String s = "";
for (int sh = 31; sh >= 0; sh--) {
s += ((n >> sh) & 1);
}
return s;
}
}
Dodatkowo musze dopisac kod obsługujący pozostałe operacje binarne (OR, XOR) oraz arytmentyczne (+, -, ...).
Wpisalem kod do eclipse i takie podstawowe bledy jak brak";" to oczywiscie wychwycilem natomiast nie wiem co ma wykonywac ten algorytm w zwiazku z czym nie wiem co zrobic z wartosciami propesedresult i realresult. Jezeli jest to mozliwe poprosze o poprawny kod (nie wime jakie zwyczaje panuja na forum) i jezeli ktos bylby na tyle mily i chociaz tak po krotce wyjasnil co ten kod wykonuje. Z gory dziekuje za odpowiedz