package Week7_Quiz;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class Week7_Quiz extends Application {
private TextField tf1;
private Label stckr;
private int revrsdNum;
private Button btn;
int num = 0;
int reverse;
public static void main(String[] args) {
launch (args);
}
@Override
public void start(Stage stage) throws Exception {
tf1 = new TextField();
tf1.setLayoutX(10);
tf1.setLayoutY(50);
stckr =new Label ("Result: ");
stckr.setLayoutX(12);
stckr.setLayoutY(100);
btn = new Button("Reverse");
btn.setLayoutX(10);
btn.setLayoutY(250);
btn.setOnAction(this::revrsdNum);
Group gp = new Group(tf1, stckr, btn);
Scene scene = new Scene(gp, 500, 650);
stage.setScene(scene);
stage.setTitle("Week 7 Quiz");
stage.show();
}
private void revrsdNum(ActionEvent t) { // need help fixing
while loop
while(num != 0){
int dig = num%10;
revrsdNum = reverse*10+dig;
num/=10;
revrsdNum = revrsdNum + tf1.getText().charAt(num);
}
stckr.setText("Result: " + revrsdNum);
}
}
I am trying to solve using this while loop but its giving me error. I need help fixing while loop and run the GUI code. Please help!! thank you
If you have any problem with the code feel free to comment. I'm assuming that the program is to reverse a number. I have uploaded the rectified method please replace it with your own, the rest of the program remains same.
revrsdNum method
private void revrsdNum(ActionEvent t) { // need help fixing while loop
//num was not initialized
num = Integer.parseInt(tf1.getText());
//setting the revrsdNum to 0 every time this method is called
revrsdNum = 0;
while (num != 0) {
int dig = num % 10;
//revrsdNum will be multiplied with 10 not reverse
revrsdNum = revrsdNum * 10 + dig;
num /= 10;
}
stckr.setText("Result: " + revrsdNum);
}
Output
Get Answers For Free
Most questions answered within 1 hours.