在java中打印出不同方法的变量? [英] Printing out variables from different methods in java?

查看:185
本文介绍了在java中打印出不同方法的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为这段代码使用不同的方法,没有java快捷方式!
这里是我的代码:

  import java.io. *; 

import java.util.Scanner;

public class pg3a {

public static void main(String [] args)throws IOException {

Scanner keyboard = new Scanner(System.in );

字符串十六进制;
char choice ='y';
boolean isValid = false;
do {
switch(choice){
case'y':
System.out.print(您想输入一个十六进制数字吗?);
System.out.print(y or n ?:);
choice = keyboard.next()。charAt(0);

System.out.print(输入一个十六进制数字:#);
hex = keyboard.next();
hex = hex.toUpperCase();
int hexLength = hex.length();
isValid =有效(十六进制);
if(isValid){
System.out.println(hex +is valid and equal to+ convert(hex));
}
else {
System.out.println(hex +is invalid。);
}
case'n':
System.out.println(quit);
}
} while(choice!='n');


public static boolean valid(String validString){

int a = 0;
if(validString.charAt(0)==' - '){
a = 1; (())='a'& b;(b)(b) & validString.charAt(i)<='f')||(validString.charAt(i)> = 0&& amp; validString.charAt(i)<= 9)))
{
返回false;
}
}
返回true;
}

如何在程序检查所有参数后十六进制数并计算它应该以十进制形式表示,它打印出十六进制数是有效的,然后十进制数是什么?

另外我怎样才能使它以^ z或^ d结尾的循环结束程序?

解决方案

要将表示十六进制数字的字符串转换为Integer,可以使用 Integer.toString(String, int); 方法:

  Integer parsedValue = Integer.parseInt(hex,16 ); 

第一个参数是要转换的字符串,第二个参数是基数规格,因此是该值现在是16位。

为了完整,Integer.toString(Integer,int)与上面相反:它将Integer值转换为指定的字符串radix。



只需创建一个名为 convert 的方法,并使其返回。



打印一个Integer不是一个大问题,您可以使用+运算符将它连接到任何字符串。

  System.out.println(The value:+ parsedValue); 

另外,请记住,您有一个小问题:



这一行使字符串中的所有字符都大写:

  hex = hex.toUpperCase(); 

但是在这里您检查小写字母:

<如果(!((validString.charAt(i)> ='a'&& amp;& validString.charAt(i)< ='f')||(validString。 charAt(i)> = 0&& validString.charAt(i)< = 9)))

要么执行 hex = hex.toLowerCase(); ,要么调整上述条件以检查是否在'A'和'F'之间。



不得不提一下,检查字符串的有效性是否被转换为数字值是不同的:它描述了一个try-catch块:尝试转换数字,如果失败,它是无效的......

 整数值; //必须在此处声明才能在try块外部访问它
try {
value = Integer.parseInt(hex,16);
$ b $ catch(NumberFormatException e){
//如果你想获得堆栈跟踪
e.printStackTrace(); //如果没有使用正确的日志框架!不要只打印它!
//处理情况:例如为用户提供重试,等等...
}


I have to use different methods for this code, no java shortcuts! Here is my code:

import java.io.*; 

import java.util.Scanner; 

public class pg3a { 

public static void main(String[] args) throws IOException { 

   Scanner keyboard = new Scanner(System.in); 

   String hex; 
   char choice = 'y'; 
   boolean isValid = false; 
   do { 
      switch (choice) { 
   case 'y': 
      System.out.print("Do you want to enter a hexadecimal number? "); 
      System.out.print("y or n?: "); 
      choice = keyboard.next().charAt(0); 

      System.out.print("Enter a hexadecimal number: #"); 
      hex = keyboard.next(); 
      hex = hex.toUpperCase(); 
      int hexLength = hex.length(); 
      isValid = valid(hex); 
        if (isValid) { 
            System.out.println(hex + " is valid and equal to" + convert(hex)); 
        } 
        else { 
           System.out.println(hex + " is invalid."); 
       } 
     case 'n': 
       System.out.println("quit"); 
      } 
      }while (choice != 'n'); 
} 

public static boolean valid (String validString) { 

  int a = 0; 
  if (validString.charAt(0) == '-') { 
  a = 1; 
} 
 for (int i=a; i< validString.length(); i++) { 
    if (!((validString.charAt(i) >= 'a' && validString.charAt(i) <= 'f')|| (validString.charAt(i) >= 0 && validString.charAt(i) <= 9))) 
{ 
return false; 
} 
} 
return true; 
} 

How can I make it so that after the program checks all the parameters for the hexadecimal number and calculates what it should be in decimal form, it prints out that the hexadecimal number is valid and then what the decimal number is??

Also how can I make it a loop that ends with either ^z or ^d to end the program?

解决方案

To convert Strings representing hexadecimal numbers to Integer, you can use the Integer.toString(String, int); method:

Integer parsedValue = Integer.parseInt(hex, 16);

The first argument is the string to be converted, the second is the radix specification, hence is this value 16 for now.

To be complete, the Integer.toString(Integer, int) is the reverse if the above: it converts an Integer value to a string in the specified radix.

Just create a method named convert, and make it return this.

Printing an Integer is not a big issue, you can just concatenate it to any String using the + operator.

System.out.println("The value: " + parsedValue);

Also, keep in mind, that you have a little problem:

This line makes all the charachters uppercase in your string:

hex = hex.toUpperCase(); 

But here you check for lowercase letters:

if (!((validString.charAt(i) >= 'a' && validString.charAt(i) <= 'f')|| (validString.charAt(i) >= 0 && validString.charAt(i) <= 9))) 

Either do hex=hex.toLowerCase();, or adjust the above condition to check to be between 'A' and 'F'.

Have to mention though that checking the validity of a String ot be converted to a numeric value is different: it tinvolves a try-catch block: try to convert the number, and if it fails, it is not valid...

Integer value; //have to declare it here to be able to access it outside of the try block
try {
   value = Integer.parseInt(hex,16);  

} catch(NumberFormatException e) {
   //if you want to get the stack trace
   e.printStackTrace(); //if not using a proper logging framework!!! Don't just print it!
   //handle the situation: e.g. break loop, write eror message, offer retry for user, etc...
}

这篇关于在java中打印出不同方法的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆