Working on an assignment for my Computer Science class. For some reason, my second System.in.read() just doesn't work, and I haven't been able to fix it or figure out why.
class ConstructedPower {
int base, exponent;
ConstructedPower(int bs, int exp) {
base = bs;
exponent = exp;
}
double getValue() {
if(exponent < 0) {
System.out.println("ERROR: exponent below 0");
return 0;
}
else return Math.pow(base, exponent);
}
double customValue() throws java.io.IOException {
int[] baseArray = {2, 10, 16};
int[] exponentArray = {1, 2, 4};
System.out.println("INPUT BASE - A = 2, B = 10, C = 16, D = EXIT");
char ch = (char) System.in.read();
if(ch == 'A') base = baseArray[0];
if(ch == 'B') base = baseArray[1];
if(ch == 'C') base = baseArray[2];
if(ch == 'D') {
System.out.println("QUITTING");
return 0;
}
System.out.println("INPUT EXPONENT - A = 1, B = 2, C = 4, D = EXIT");
ch = (char) System.in.read(); // this usually doesn't trigger. why? I have no idea!
if(ch == 'A') exponent = exponentArray[0];
if(ch == 'B') exponent = exponentArray[1];
if(ch == 'C') exponent = exponentArray[2];
if(ch == 'D') {
System.out.println("QUITTING");
return 0;
}
return Math.pow(base, exponent);
}
}
public class SanePower {
public static void main(String[] args) throws java.io.IOException {
System.out.println("Initializing...");
ConstructedPower newPower = new ConstructedPower(2, 2);
double a = newPower.getValue();
System.out.println(a);
double b;
do {
b = newPower.customValue();
System.out.println(b);
}
while(b != 0);
}
}
I hate to just dump a bug on the thread like this, but it's too late in the evening to contact the professor, the assignment is (weirdly) due at midnight, and I legitimately have no clue what's causing this.
edit: never mind, figured it out. I feel pretty silly now.