java 获取温度,Java中温度程序的类声明

阅读: 评论:0

java 获取温度,Java中温度程序的类声明

java 获取温度,Java中温度程序的类声明

So this is the problem I'm having I need to input these five methods:

Temperature – a constructor method. This should set the initial temperature to 100.

getTemp – a method to return the current stored value of degrees Celsius

convertToF – a method to convert a Celsius temperature to degrees Fahrenheit

convertToK – a method to convert a Celsius temperature to degrees Kelvin

updateTempC – a method to update the stored temperature in degrees Celsius

into this program:

import java.util.Scanner;

public class TempProg {

public static void main(String[] args)

{

// Declare objects

Scanner scan = new Scanner(System.in);

Temperature tempConv = new Temperature();

// Declare variables

int newTemp;

boolean entryValid;

// Declare constants

final int MIN_TEMP = -273;

final int MAX_TEMP = 10000;

System.out.println("tTemperature converter");

// Set a dummy selection value, so that we always show the options on the first go

char selection = 'x';

// Offer a list of options

while (selection != 'q') {

System.out.println("ntCurrent temperature in degrees C: " + Temp());

System.out.println("tType f to display temperature in Fahrenheit");

System.out.println("tType k to display temperature in Kelvin");

System.out.println("tType c to set a new temperature");

System.out.println("tType q to quit");

// Read from the keyboard

selection = ().charAt(0);

// Act on the selection

switch(selection) {

case 'f': // Print Fahrenheit version

System.out.println("nt" &#Temp()+ " degrees C = "&#vertToF() +" degrees F" );

break;

case 'k': // Print Kelvin version

System.out.println("nt" &#Temp()+ " degrees C = "&#vertToK() +" degrees K" );

break;

case 'c': // Get a new temperature

entryValid=false; // Reset entryValid for this round

/* test for !entryValid

* i.e. "not entryValid"

* i.e. same as "entryValid == false"

*/

while (!entryValid) { // This will always be true the first time

System.out.print("ntPlease enter a new temperature: ");

newTemp = Int();

// Check validity of new temperature

if (newTemp < MIN_TEMP || newTemp > MAX_TEMP) {

System.out.println("tPlease enter a valid temperature");

} else {

entryValid=true;

tempConv.updateTempC(newTemp);

}

}

break;

case 'q': // Don't do anything for q, we print a message later

break;

default: // If it is not f, k, c or q then default is error message

System.out.println("ntOption " + selection + " not understood");

}

}

System.out.println("ntPROGRAM ENDED");

}

}

and this is what I have

private double Temperature (double currentTemp)

{

currentTemp = 100;

return currentTemp;

}

public double convertToF (double TempF)

{

TempF = ((9 * currentTemp) / 5 ) + 32;

return TempF();

}

public double convertToK (double TempK)

{

TempK = currentTemp + 273;

return TempK();

}

public void updateTempC (double currentTemp)

{

newTemp = currentTemp;

return currentTemp();

}

public double getTemp()

{

return currentTemp;

}

it is basically won't compile and I'm 99% sure it's very wrong and I literally have no idea what suggestions?

and errors are:

tempProg.java:14: error: class TempProg is public, should be declared in a file named TempProg.java

public class TempProg {

^

tempProg.java:26: error: cannot find symbol

TempF = ((9 * currentTemp) / 5 ) + 32;

^

symbol: variable currentTemp

location: class TempProg

tempProg.java:27: error: cannot find symbol

return TempF();

^

symbol: method TempF()

location: class TempProg

tempProg.java:32: error: cannot find symbol

TempK = currentTemp + 273;

^

symbol: variable currentTemp

location: class TempProg

tempProg.java:33: error: cannot find symbol

return TempK();

^

symbol: method TempK()

location: class TempProg

tempProg.java:38: error: cannot find symbol

newTemp = currentTemp;

^

symbol: variable newTemp

location: class TempProg

tempProg.java:40: error: cannot return a value from method whose result type is void

return currentTemp();

^

tempProg.java:45: error: cannot find symbol

return currentTemp;

^

symbol: variable currentTemp

location: class TempProg

tempProg.java:54: error: cannot find symbol

Temperature tempConv = new Temperature();

^

symbol: class Temperature

location: class TempProg

tempProg.java:54: error: cannot find symbol

Temperature tempConv = new Temperature();

^

symbol: class Temperature

location: class TempProg

解决方案

You need a Temperature class. I'm just guessing that you've added those methods to the TempProg considering the improper "constructor". What you need is another class all together

public class Temperature {

// Put those method and data members here

}

The constructor should not return a value. It should public Temperature(), or some variation depending on your specific requirements.

本文发布于:2024-02-02 14:41:18,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170685607944478.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:温度   声明   程序   java   Java
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23