31
General Discussion / Re: if self.isCoder(): post() #Programming Thread
« on: August 18, 2016, 07:29:57 pm »
Desktop but more of a school based project thing (not that i'm currently a student).
March 6, 2024: Dwarf Fortress 50.12 has been released.
News: February 3, 2024: The February '24 Report is up.
News: February 4, 2021: Dwarf Fortress Talk #28 has been posted.
News: November 21, 2018: A new Threetoe story has been posted.
Forum Guidelines
The faction you're referring to is the son of bitches, or SOB. They're part of the church, wear power armor, wield chainswords and bolters, and freakin magic, and will rek ur face.The Sisters of Battle do not use magic. They are very much a "burn the witch" organisation.
@3man75:I have no clue why it's...Oh! When you close a looping statement, all the variables declared in it go away. So your Scanner is already dead, and honestly you probably don't need to close it anyway.
import java.util.Scanner;
public class MainMenu {
public static void main(String[] args) {
[b]do {[/b]
System.out.println("Welcome to dice roller. Please input the number of the dice you want to roll. For example: 6 for a 6 sided dice or 50 for a 50 sided dice.");
Scanner howManySides = new Scanner (System.in);
int sidesOfADice = howManySides.nextInt();
ObjectDice gameDice = new ObjectDice (sidesOfADice); //One argument calling constructor.
sidesOfADice = howManySides.nextInt();//reusing scanner. sidesOfADice is a int holder for the scanner.
gameDice.rollMulti(sidesOfADice);
} while (sidesOfADice != 0);
howManySides.close();
}
}@3man75:What does your code look like now?
@Mephisto:No he does not.nevermind
Also, 3man75, good grief, you NEVER need to import Object.
I want to help with this, but I'd end up completely rewriting your code and not really helping.
Also, you really, really, really need to learn more about how to write readable variable names...AND STOP MAKING MULTIPLE SCANNERS POINTED AT SYSTEM.IN FOR LIKE THE TENTH OR SO TIME AAAUGH. Sorry. I shouldn't shout. But you only need the one Scanner, let's call it input, and then you can call input.nextInt() multiple times.
...Also, screaming about THIS METHOD IS BEING CALLED BY MAIN! is...Not a guaranteed truth, in addition to being really weird documentation-wise, because the line of code you wrote ISN'T being called in main, it's being called in your method. One's code should not depend on being called by certain other code unless it's a private method, which I will not talk about because that's outside your current scope.
...Sod it, I'm going to write an unconnected example in the hopes that it actually gets through.Code: [Select]import java.util.Scanner;
public class Demo{
Scanner scan = new Scanner(System.in); //And notice how there is ONE Scanner, not two or three.
public static void main(String[] args){
//I'll avoid complicating things and just write it to go through each method once, though simply wrapping this with a while loop and implementing some sort of menu shouldn't be hard.
System.out.println("Input the initial value for instancevariable");
DemoObject whatdoesitdo = new DemoObject(scan.nextInt()); //Much less code to write than creating a new variable to store a value you only use once.
//Now, to demonstrate proper object-based I/O.
System.out.println("Called singleUseThing and got "+whatdoesitdo.singleUseThing()); //This is what a return statement is for. It hands data back to the method that called things.
System.out.println("How many times would you like to call multiUseThing?");
int[] output; //Have to declare this outside of the loop so it doesn't vanish after the first iteration.
for(int count = 0, int max = scan.nextInt() /*Reusing a Scanner, see? Also, you can do this comma thing here.*/, output = whatdoesitdo.multiUseThing(max); count<max; count++){
System.out.println("Result "+(count+1)+" of multiUseThing was "+output[count]);
}
}
}
public class DemoObject{
int instancevariable;
Random RNGesus = new Random();
DemoObject(int setup_value){
instancevariable=setup_value;
}
public void setInstanceVariable(int to_set){
instancevariable = to_set;
}
public int singleUseThing(){
return RNGesus.nextInt()*instancevariable;
}
public int[] multiUseThing(int num_times){
int[] temp_storage = new int[num_times];
for(int count = 0; count<num_times; count++){
temp_storage[count] = RNGesus.nextInt()*instancevariable;
}
return temp_storage; //Handing the array back for whatever. If you only wanted to print odd-numbered results, you could do that without mucking up the object for uses that need all the results printed.
}
I apologize for the tabs, but it was much faster than spaces, especially because I typed this on my phone
Good use with the prints for debugging, you get a much better idea of the order that bits of code are being called.
Here's what I like to do: have the debug print at the start of a function print out it's name and parameters. Include the class name as well for the most detail. e.g. for your dice constructor, instead of "hello", try printing:
System.out.println("Dice.Dice("+x+")");
Debug printing of the name of functions and what values they are getting can really save time when you know some bit of code is screwing up, but you're not sure where the problem is. I've had countless times that I've debugged some "faulty" bit of code over and over, only to do a debug and realize that another bit of code was sending it the wrong values. The first thing you should always double-check is what values a function is receiving.
