Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1924

Re: My first program = Quiz :D

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.help
Subject Re: My first program = Quiz :D
Date 2012-07-01 10:20 -0700
Organization albasani.net
Message-ID <jsq0s7$aoi$1@news.albasani.net> (permalink)
References <9c74bdc0-3238-4124-afcf-154eb305e5ae@googlegroups.com> <Cb-dnTP1TplI5m3SnZ2dnUVZ_hudnZ2d@earthlink.com> <e50579ae-836c-46c5-952b-0cfdbfcbd7a8@googlegroups.com>

Show all headers | View raw


ericmiranda7... wrote:
> Patricia Shanahan wrote:
>> ericmiranda7... wrote:
>>> Alright, here is my first program. Just testing it.
>>>
>>> ----------------------------------------
>>> import java.util.Scanner;
>>> public class quiz {

Just a side note, but when you're beginning it's better to establish good 
habits than to have unlearn bad ones once you're more knowledgeable. In this 
case I'm referring to source-code formatting conventions, which are 
more-or-less universal for Java based on the standard in play since 1999.
<http://www.oracle.com/technetwork/java/codeconv-138413.html>

To fix your code to comply, spell type names (like 'quiz') with an initial 
upper-case letter ('Quiz').

Indent 2 or 4 spaces per logical nesting level. (Never use TAB characters to 
indent).

Fill out your Javadoc comments (the ones that start with /** ) completely.

Get rid of your template "// TODO Auto-generated" lines.

I'll return to your actual question below.

>>> 	/**
>>> 	 * @param args
>>> 	 */
>>> 	public static void main(String[] args) {
>>> 		// TODO Auto-generated method stub
>>> Scanner myScanner = new Scanner(System.in);
>>>
>>> String thisString;

Declare variables at the point of use. And use meaningful names. It isn't 
important that this is "this string" but that it represents user input.

>>> double points = 0;

Just '0', with no decimal point, is an 'int' constant. It's better style to 
initialize a double variable with a double constant.

>>> double total;
>>>
>>> System.out.println("Did Germany win vs. Portugal? Yes or no");
>>> thisString = myScanner.nextLine();

     String userInput = myScanner.nextLine();

>>> if (thisString.equals("Yes")) {
>>> 	System.out.println("That is correct! Plus points!");
>>> 	total = points + 1;

See how you changed the value of 'total'? You can do the same sort of thing 
with 'points'.

     points = points + 1;

Of course, 'points' will need an initial value for that to work.

>>> 	System.out.println("Is the Galaxy Tab android?");
>>> 	thisString = myScanner.nextLine();
>>> 	if (thisString.equals("Yes")) {
>>> 		System.out.println("That is correct. Plus points!");
>>> 		total = points + 1;
>>> }
>>> }
>>> 	}
>>> }
>>> ---------------------------------------------------------
>>> Now, my question is -
>>> I wrote the statement(total = points + 1;)twice. Even though I answered "yes
>>> (the correct answer to each question)twice my total points came to 1. Should they not add up and become two? Thank-you for the answers.
>>
>> points is always zero - none of your code changes it - so (points + 1)
>> is always one.
>>
>> Why the separate variables "points" and "total"?
>>
>> Patricia

Don't quote sigs.

> I'm a newbie, and I really don't know why I did that. Could you tell me how to update the points variable? I'd much appreciate it. Thanks.

Don't repeat the entire post twice.

> On Sunday, July 1, 2012 10:03:25 PM UTC+5:30, Patricia Shanahan wrote:
>> On 7/1/2012 9:18 AM, ericmiranda7@gmail.com wrote:
>>> Alright, here is my first program. Just testing it.
>>>
>>> ----------------------------------------
>>> import java.util.Scanner;
>>> public class quiz {
... [snip] ...
>>
>> Why the separate variables "points" and "total"?

Don't repeat the entire post three times.

> On Sunday, July 1, 2012 10:03:25 PM UTC+5:30, Patricia Shanahan wrote:
>> On 7/1/2012 9:18 AM, ericmiranda7@gmail.com wrote:
>>> Alright, here is my first program. Just testing it.
>>>
>>> ----------------------------------------
>>> import java.util.Scanner;
>>> public class quiz {
... [snip] ...
>>
>> Why the separate variables "points" and "total"?

My God, man!

> On Sunday, July 1, 2012 10:03:25 PM UTC+5:30, Patricia Shanahan wrote:
>> On 7/1/2012 9:18 AM, ericmiranda7@gmail.com wrote:
>>> Alright, here is my first program. Just testing it.
>>>
>>> ----------------------------------------
>>> import java.util.Scanner;
>>> public class quiz {
>>>
... [snip] ...
>>
>> Why the separate variables "points" and "total"?

Are you quite certain you posted the same material enough times?

Read the official tutorials.
<http://www.oracle.com/technetwork/java/index-jsp-135888.html>

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

My first program = Quiz :D ericmiranda7@gmail.com - 2012-07-01 09:18 -0700
  Re: My first program = Quiz :D Patricia Shanahan <pats@acm.org> - 2012-07-01 09:33 -0700
    Re: My first program = Quiz :D ericmiranda7@gmail.com - 2012-07-01 09:47 -0700
      Re: My first program = Quiz :D Patricia Shanahan <pats@acm.org> - 2012-07-01 09:57 -0700
      Re: My first program = Quiz :D markspace <-@.> - 2012-07-01 10:11 -0700
      Re: My first program = Quiz :D Lew <noone@lewscanon.com> - 2012-07-01 10:20 -0700
  Re: My first program = Quiz :D ericmiranda7@gmail.com - 2012-07-01 10:20 -0700
    Re: My first program = Quiz :D Patricia Shanahan <pats@acm.org> - 2012-07-01 10:59 -0700
      Re: My first program = Quiz :D Roedy Green <see_website@mindprod.com.invalid> - 2012-07-03 05:16 -0700
  Re: My first program = Quiz :D Roedy Green <see_website@mindprod.com.invalid> - 2012-07-01 17:02 -0700

csiph-web