The program whose results depend on the Yes or No option and the number entered in Java does not work well in the if syntax.

Asked 2 years ago, Updated 2 years ago, 32 views

I am a beginner in Java.
Depending on the current Yes or No option and the number (age) entered,

Yes 11-60
No. 12-59
No 0-2
No. 3-11
No. 12-59

I'm trying to create a practice that branches each of with an if syntax to produce different sentences and photos, but I get confused when I type in Yes 11-60 and No 12-59.

What should I do if the numbers are the same but the Yes/No input is different?

import java.awt.Graphics;
import javax.swing.*;

public class conditionalLab extensions JPanel {
    
    intage;
    ImageIcon picture1;
    ImageIconpicture2;
    ImageIcon picture3;
    ImageIconpicture4;
    int favorite;
    
    public conditionalLab(){
        // Your custom initialization code here
        JOptionPane.showConfirmDialog(null,
                                    "Are you Student or Military?"
                                    "Laie Palms Cinemas",
                                    JoptionPane.YES_NO_OPTION);
                                    
        final String Mike = JoptionPane.showInputDialog("Please enter your age.");
        age=Integer.parseInt(mike);
        
        picture1 = new ImageIcon("dolittle.png");
        picture2 = new ImageIcon("frozen2.png");
        picture3 = new ImageIcon("starwars9.png");
        picture4 = new ImageIcon("Daisy.png");
    }

    @ Override
    public void paintComponent (Graphics g) {
        // Your custom rendering code here
        if((favorite==JOptionPane.YES_OPTION)&(age>=11)&(age<=60)){
            g.drawString("That will be $8.00, please", 10,30);
            picture3.paintIcon(null,g,10,50);

        } else if ((favorite==JoptionPane.NO_OPTION)&(age>=0)&(age<=2){
            g.drawString("You get free!", 10, 10);
            picture1.paintIcon(null,g,10,50);

        } else if ((favorite==JoptionPane.NO_OPTION)&(age>=3)&(age<=11)){
            g.drawString("Youowe $7.00!",10,10);
            picture2.paintIcon(null,g,10,50);

        } else if ((favorite==JoptionPane.NO_OPTION)&(age>=12)&(age<=59){
            g.drawString("Youowe $10.00!",10,10);
            picture3.paintIcon(null,g,10,50);

        } else if ((favorite==JoptionPane.NO_OPTION)&(age>=60)&(age<=119){
            g.drawString("That will be $7.00, please", 10, 10);
            picture4.paintIcon(null,g,10,50);

        } else if ((favorite==JoptionPane.NO_OPTION)&(age>=120)&(age<=-1){
            g.drawString("Are you sure you typed that correctly?", 10, 10);
           
        } 
    
    }

    public static void main(String[]args) {
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(300,300);
        window.setContentPane(new conditionalLab());
        window.setVisible(true);
    }
}
If you enter one of the numbers 12-59 with No:
Youowe $10.00!

If you enter one of the 11-60 numbers in Yes:
That will be $8.00, please"
If you enter one of the numbers 12-59 with No:
That will be $8.00, please

If you enter one of the 11-60 numbers in Yes:
That will be $8.00, please

java

2022-09-30 16:56

2 Answers

The revised content and the last elsif syntax in the comment are modified to the else syntax, and the code that worked as originally intended is listed below.Thank you for your help this time!

import java.awt.Graphics;
import javax.swing.*;

public class conditionalLab extensions JPanel {

    intage;
    ImageIcon picture1;
    ImageIconpicture2;
    ImageIcon picture3;
    ImageIconpicture4;
    int jobs;

    public conditionalLab(){
        // Your custom initialization code here
        jobs=JOptionPane.showConfirmDialog(null,
                                    "Are you Student or Military?"
                                    "Laie Palms Cinemas",
                                    JoptionPane.YES_NO_OPTION);

        final String Mike = JoptionPane.showInputDialog("Please enter your age.");
        age=Integer.parseInt(mike);

        picture1 = new ImageIcon("dolittle.png");
        picture2 = new ImageIcon("frozen2.png");
        picture3 = new ImageIcon("starwars9.png");
        picture4 = new ImageIcon("Daisy.png");
    }

    @ Override
    public void paintComponent (Graphics g) {
        // Your custom rendering code here

        if((jobs==JOptionsPane.YES_OPTION)&(age>=11)&(age<=60)){
            g.drawString("That will be $8.00, please", 10,30);
            picture3.paintIcon(null,g,10,50);

        } else if ((jobs==JOptionsPane.NO_OPTION)&(age>=0)&(age<=2){
            g.drawString("You get free!", 10, 10);
            picture1.paintIcon(null,g,10,50);

        } else if ((jobs==JOptionsPane.NO_OPTION)&(age>=3)&(age<=11)){
            g.drawString("Youowe $7.00!",10,10);
            picture2.paintIcon(null,g,10,50);

        } else if ((jobs==JoptionPane.NO_OPTION)&(age>=12)&(age<=59){
            g.drawString("Youowe $10.00!",10,10);
            picture3.paintIcon(null,g,10,50);

        } else if ((jobs==JOptionsPane.NO_OPTION)&(age>=60)&(age<=119){
            g.drawString("That will be $7.00, please", 10, 10);
            picture4.paintIcon(null,g,10,50);

        } else{
            g.drawString("Are you sure you typed that correctly?", 10, 10);

        }  

    }

  public static void main(String[]args) {
      SwingUtilities.invokeLater(()->{
          JFrame window = new JFrame();
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.setSize(300,300);
          window.setContentPane(new conditionalLab());
          window.setVisible(true);
   });
}


2022-09-30 16:56

I think the code written in the answer is the latest version, so I will write it based on this.

1.

Perhaps the intention is to determine the following conditions:

On the other hand, the code in the answer has the following structure:

Based on the code provided in the answer, I don't think "Are you sure you typed that correctly" was the result of "Are you sure that correct?"&=60)&(age<=119).

(code difference)

2.

Swing components (such as JFrame, JPanel, and JoptionPane) must be manipulated by a thread called Principle Event Dispatch Thread.

In this code, the Swing operation is written in the main method, so that part will be processed in the main thread.

As per the link above, you can use the SwingUtilities.invokeLater method.

public static void main(String[]args){
    SwingUtilities.invokeLater(()->{
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(300,300);
        window.setContentPane(new conditionalLab());
        window.setVisible(true);
    });
}

(code difference)


2022-09-30 16:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.