This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kaggs
Kaggs wrote ...

2020/8/12

Greenfoot random number with it duplicating

Kaggs Kaggs

2020/8/12

#
public int getRandomNumber(int start,int end) { int normal = Greenfoot.getRandomNumber(end-start+1); return normal+start; } hile (numQuestion > 0) { switch(getRandomNumber(1,numQuestion)) { case 1: System.out.println("Accused money launderers tried to move $1 million in cooler bags: True or False"); break; case 2: System.out.println("Corona virus is not deadly: True or False "); break; case 3: System.out.println("Man charged over alleged Sydney CBD hammer attack } numQuestion = numQuestion - 1; } numQuestion is just a number between 2-5 i dont want the question duplicating
danpost danpost

2020/8/12

#
Seems like what you really want is a random order to ask the questions in. I would suggest you use a list that can be shuffled and have elements removed from. The elements can be Integer objects or of Question objects. A simple Question class might be:
public class Question extends Object
{
    String question;
    boolean answer;
    
    public Question(String q, boolean a)
    {
        question = q;
        answer = a;
    }
}
where in MyWorld, you might use this:
/**  imports needed  */
import java.util.Collections;
import java.util.ArrayList;

/**  global  */
private ArrayList<Question> questions = new ArrayList<Question>();
private Question currentQuestion;

/**  in constructor ( public MyWorld() )  */
Question q = new Question("Accused money launderers tried to move $1 million in cooler bags: True or False", true);
questions.add(q);
q = new Question("Corona virus is not deadly: True or False ", true);
questions.add(q);
q = new Question("Man charged over alleged Sydney CBD hammer attack. True or False.", false);
questions.add(q);
// ...
Collections.shuffle(questions);

/**  in act  */
if (currentQuestion == null) askQuestion();
if (currentQuestion == null) Greenfoot.stop(); // or testCompleted();
// upon answering
currentQuestion = null;

/** askQuestion method  */
private void askQuestion()
{
    if (questions.isEmpty()) return;
    currentQuestion = questions.remove(0);
    System.out.println(currentQuestion.question);
}
Kaggs Kaggs

2020/8/12

#
is it possiable to do it with out an array this is for an assessment and im stuck
Kaggs Kaggs

2020/8/12

#
the assessment is to make a game where they can pick the amount of question they went to answer from 2 being the min and 5 being the max
danpost danpost

2020/8/12

#
Kaggs wrote...
is it possiable to do it with out an array this is for an assessment and im stuck
Yes. The code will be more simple, but much more of it.
/** global */
Question q1, q2, q3, q4, q5;
Question currentQuestion;

/**  in constructor ( public MyWorld() )  */
Question q1 = new Question("Accused money launderers tried to move $1 million in cooler bags: True or False", true);
Question q2 = new Question("Corona virus is not deadly: True or False ", true);
Question q3 = new Question("Man charged over alleged Sydney CBD hammer attack. True or False.", false);
// ...

/**  in act  */
if (currentQuestion == null) askQuestion();
if (currentQuestion == null) Greenfoot.stop();
//
if (<< question answered >>) currentQuestion = null;

/** askQuestion method */
private void askQuestion()
{
    int questionsLeft = 0;
    if (q1 != null) questionsLeft++;
    if (q2 != null) questionsLeft++;
    if (q3 != null) questionsLeft++;
    if (q4 != null) questionsLeft++;
    if (q5 != null) questionsLeft++;
    if (questionsLeft == 0) return;
    int rand = 1+Greenfoot.getRandomNumber(questionsLeft);
    if (q1 != null && --rand == 0)
    {
        currentQuestion = q1;
        q1 = null;
    }
    if (q2 != null && --rand == 0)
    {
        currentQuestion = q2;
        q2 = null;
    }
    if (q3 != null && --rand == 0)
    {
        currentQuestion = q3;
        q3 = null;
    }
    if (q4 != null && --rand == 0)
    {
        currentQuestion = q4;
        q4 = null;
    }
    if (q5 != null && --rand == 0)
    {
        currentQuestion = q5;
        q5 = null;
    }
    System.out.println(currentQuestion.question);
}
Kaggs Kaggs

2020/8/13

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.*; 
import java.util.Random; 
/**
 * Write a description of class Button here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Button extends Actor
{
    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
       Object[] options = {"News Quiz Game", "Fake Count Game"};
       int i = JOptionPane.showOptionDialog(null, "What game mode would you like to play","Quiz Game",
       JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]); 
       
       if (i == 1)
       { 
         System.out.println("you have picked Fake games") ;
         FakeGames(); 
       }
       
       
    }   
    /**
     * this method will be used for Fake games 
     */
    public void FakeGames()
    {
      String str;
      int numQuestion;
      int num1 = 0 , num2 = 0, num3 = 0, num4 = 0, num5 = 0; 
      int Rand = 0 ;
      while (true)
      {
       str = JOptionPane.showInputDialog("input the amount of question you went to try between 2-5"); 
       numQuestion = Integer.parseInt(str); 
       if (numQuestion <= 5 && numQuestion >=2)
       {
           break; 
           
       }
       
       }
       
      while (numQuestion > 0) 
      {
     
           
       switch(getRandomNumber(0,numQuestion)) 
       {
          case 0: 
          System.out.println("Accused money launderers tried to move $1 million in cooler bags: True or False");
           
          break; 
          case 1: 
          System.out.println("Corona virus is not deadly: True or False "); 
          break;
          case 2:  
          System.out.println("Man charged over alleged Sydney CBD hammer attack
break; 
          case 3: 
          System.out.println("Wearing a neck gaiter is better then normal mask: True or False 
reak;
          case 4:
          System.out.println("Man taken to hospital after alleged crossbow shooting: True or False
}
numQuestion = numQuestion - 1;
        
      }
      


    }
    /** 
     * produce a randome number from the selected number (num1, num2) 
     */
    public int getRandomNumber(int start,int end)
    {
       int normal = Greenfoot.getRandomNumber(end-start+1);
       return normal+start;
    }
   
}
this is what i have so far. the code that you have given me i dont really understand the Question q1, q2, q3, q4, q5; Question currentQuestion; scince i dont know where it goes. also is there a simple way to just get 5 randome number that is unique
Kaggs Kaggs

2020/8/13

#
Display the news items to the console window or use a dialog box. Each news item presented in the game must be unique. Ask the user to count the fake news items out of the presented list. If the user correctly guesses the number of fake news items, the program should display a congratulations message. If not, the program should display each individual news items presented in the game along with its true/fake status to the console window this is the question
danpost danpost

2020/8/13

#
Kaggs wrote...
is there a simple way to just get 5 randome number that is unique
Well, I guess your code could do the shuffling. Say you had 5 global fields with 1 thru 5 in them; then, something like this:
/**  global */
int a=1, b=2, c=3, d=4, e=5;

/** shuffle method */
private void shuffle()
{
    int f=0;
    for (int i=0; i<100; i++)
    {
        switch(Greenfoot.getRandomNumber(10))
        {
            case 0: f=a; a=b; b=f; break;
            case 1: f=a; a=c; c=f; break;
            case 2: f=a; a=d; d=f; break;
            case 3: f=a; a=e; e=f; break;
            case 4: f=b; b=c; c=f; break;
            case 5: f=b; b=d; d=f; break;
            case 6: f=b; b=e; e=f; break;
            case 7: f=c; c=d; d=f; break;
            case 8: f=c; c=e; e=f; break;
            case 9: f=d; d=e; e=f; break;
        }
    }
}
Then you could switch question number, which is incremented, to redirect to the real one, to switch for the question itself:
int qNum = 0;
switch (numQuestion)
{
    case 1:  qNum = a; break;
    case 2:  qNum = b; break;
    case 3:  qNum = c; break;
    case 4:  qNum = d; break;
    case 5;  qNum = e; break;
}
switch (qNum) // your line 56 above
{
    // ...
}
Kaggs Kaggs

2020/8/13

#
i have hard coded it so that there are no duplicate
while (numQuestion > 0) 
      {
          int Randnum = Greenfoot.getRandomNumber(5);
          //System.out.println(Randnum) ; 
     
           
       switch(Randnum) 
       {
          case 0: 
          if (num1==0)
          {
            numQuestion = numQuestion +1;  
            //System.out.println("duplicate1"); 
            
            break;
            
          }
           else 
           {
              // System.out.println("new number") ; 
               System.out.println("Accused money launderers tried to move $1 million in cooler bags: True or False");
               num1 = Randnum;  
           }                     
          break; 
          case 1: 
          if (num2==1)
          {
            numQuestion = numQuestion + 1; 
            break;
            
          }
          else 
          {
            System.out.println("Corona virus is not deadly: True or False "); 
            num2 = Randnum; 
          }
          break;
          case 2: 
          if (num3==2)
          {
            numQuestion = numQuestion + 1; 
            break;
            
          }
          else
          {
          System.out.println("Man charged over alleged Sydney CBD hammer attack
You need to login to post a reply.