arrays - Asking the user to think of, and write down, a number between 1 and 1,000,000 in java -
hi guys i'm working on writing program that's asking user think of , write down number between 1 , 1,000,000.
then asks questions "is greater 500000?", , user answers "yes" or "yes" or "no or "no", or "got it!" i'm stuck at. don't know how ask questions code got far :
import javax.swing.joptionpane; public class questions { public static void main(string[] args) { joptionpane.showmessagedialog(null, questions"); string s = joptionpane.showinputdialog("enter number between 1 , 1,000,000"); int = 1000000; if (i>500000){ } }
how use joptionpane.showinputdialog
ask questions , answers ? appreciated.
first of all, recommend debugging in console until logic complete, work on gui.
you trying implement binary search, computer should answer right in log(n)
guesses.
here code started. gui should straightforward. read appropriate javadocs.
try (scanner k = new scanner(system.in);) { string input; int lo = 0; int hi = 1000000; int mid; while (true) { mid = (lo + hi) / 2; system.out.printf("is equal %d?\n", mid); if (!input.equalsignorecase("got it!")) { break; } else { system.out.printf("is greater %d?\n", mid); input = k.nextline(); if (input.equalsignorecase("yes")) { lo = mid; } else { hi = mid; } } } } catch (ioexception e) { system.err.println(e.getmessage()); }
Comments
Post a Comment