Quick ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Quick ?

  1. #1
    Join Date
    Mar 2009
    Posts
    14

    Question Quick ?

    I am trying to print out the temperature and the day so it would look like this:
    Day 0 had temperature 88.0 which was above average
    I can't figure out why it won't print thanks

    import java.util.Scanner;
    public class NumberAboveAverage{

    public static void main(String args[]) {
    Scanner input = new Scanner(System.in );
    int day[]; //declares array named day
    int temperature[]= {88,89,75,78,98,56,32,78,45,54};//creating the array temperature

    System.out.printf("%s%8s\n", "Day", "Temperature");

    for (int counter=0; counter < temperature.length; counter++)
    System.out.printf("%5d%8d\n",counter, temperature[counter] );

    double Average = 69.3;
    System.out.println("Enter day number(0-10)");
    int Day=input.nextInt();
    System.out.printf("Day %s had temperature %5d%8d which was above average", Day,temperature[]);

  2. #2
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: Quick ?

    Code:
    int day[]; //declares array named day
    In Java, the fact that it's an array becomes part of the type, so we usually say int[] day; rather than int day[]. You never actually use day anyway, though.

    Code:
    System.out.printf("&#37;s%8s\n", "Day", "Temperature");
    This should yield "DayTemperature\n" — 8 is not wide enough to contain all of "Temperature", much less separate it from "Day".

    Code:
    System.out.printf("Day %s had temperature %5d%8d which was above average", Day,temperature[]);
    Day is an integer, but you've used the string format specifier %s. temperature[] is invalid syntax here: you need to specify which index of temperature you mean, which I presume is Day. However, this string will only ever say that the temperature is above average, even if it isn't! You need to add cases for below and equal to average as well.

    Code:
    import java.util.Scanner;
    
    public class NumberAboveAverage {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
    
            int day;
            int[] temperatures = {88, 89, 75, 78, 98, 56, 32, 78, 45, 54};
            final double AVERAGE = 69.3;
    
            System.out.printf("%s\t%s\n", "Day", "Temperature");
    
            for (int i = 0; i < temperatures.length; ++i)
                System.out.printf("%2d\t%6d\n", i, temperatures[i]);
    
            System.out.print("\nEnter day number (0-9): ");
            day = input.nextInt();
    
            System.out.printf("Day %d had temperature %d, which was%s average.\n",
                              day,
                              temperatures[day],
                              temperatures[day] > AVERAGE ? " above"
                                : temperatures[day] < AVERAGE ? " below"
                                : "");
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center