如何从java中的用户输入获取int的长度

时间:2016-10-21 19:25:51

标签: java

我正在尝试获取用户输入的字符长度。如果用户输入长度小于5或大于5的字符串,则应输入“输入有效的5位数字”。如果长度是5,那么我希望它继续下一个问题,但由于某种原因,它不起作用。谢谢!

package Hw;
import java.util.Scanner;
public class Library {

public static void main(String[] args) {

int  cardnumber = 0;
int age;
int item = 0;
int overdue = 0;

int fine = item * overdue;

int length = length(Integer.toString(cardnumber));

Scanner Keyboard = new Scanner (System.in);



System.out.println("Enter Card Number: ");
cardnumber = Keyboard.nextInt();
System.out.println("Enter Age: ");
        age = Keyboard.nextInt();
        System.out.println("Enter Item Type (Book = 1, Magazine = 2, Dvd = 3): ");
        item = Keyboard.nextInt();
        System.out.println("Enter The Amount Of Days The Item Has Been Overdue");
overdue = Keyboard.nextInt();

if (length == 5)
{
System.out.println(cardnumber);
}
if (length > 5 || length < 5)
{
System.out.println("Enter A Valid 5 Digit Number");
}



if (item == 1)
{
fine = (int) (overdue * 0.50);
}

else if (item == 2)
        {
            fine = (int) (overdue * 0.25);
        }

    else if (item == 3)
        {
        fine = (int) (overdue * 1.50);
        }



if (age > 5 || age < 18)
{
fine = (int) 1.00;
}


if (age > 70)
{
fine = 0;
}


else if(age > 18 || age < 70)
{

if(overdue < 5)
System.out.println("Days Overdue "+ overdue);
}

else
{
fine = (int) 5.00;
}


/*else if (age < 6 || age > 17)
{
fine = (int) 1.00;
}
*/



System.out.println("Card Number "+ cardnumber);



System.out.println("Age "+ age);

//  System.out.println("Days overdue "+ overdue);

System.out.println("Fine "+ fine);

//  System.out.println("Fine adjustment reason "+ )


if(item == 1)
{
System.out.println("item Book");
}

else if(item == 2)
{
System.out.println("item Magazine");
}

else if(item == 3)
{
System.out.println("item Dvd");
}

else if(item <1 || item >3)
{
System.out.println("Invalid Information");
}   
}

private static int length(String string) {
// TODO Auto-generated method stub
return 0;
}
}

4 个答案:

答案 0 :(得分:0)

主要概念应该是使用String而不是Integer,通过这种方法,检查用户的输入长度会更容易和更快。

我建议更改代码如下: 改变那一行:

int length = length(Integer.toString(cardnumber));

为:

String length = Integer.toString(cardnumber); 

另一个变化是以下几行:

if (length == 5)
{
System.out.println(cardnumber);
} 
if (length > 5 || length < 5)
{
System.out.println("Enter A Valid 5 Digit Number");
}

应更换为:

while(length != 5) {
    System.out.println("Enter A Valid 5 Digit Number");
    cardnumber = Keyboard.nextInt();
    length = Integer.toString(cardnumber);
}

答案 1 :(得分:0)

问题的关键在于如何实施此方法:

private static int length(String string) {
    // TODO Auto-generated method stub
    return 0;
}

你想要的是查看Java字符串对象中内置的length()方法。

为了提供帮助,您的代码中似乎还有其他一些误解。如果我在您提供的代码中阅读过多,请道歉,但看起来您需要考虑一下该行

int length = length(Integer.toString(cardnumber));

确实 - 或者,在您运行该行时存储的值cardnumber

答案 2 :(得分:0)

您可以使用简单的数学方法来做到这一点:

private static int getLength(int num) {

    int count = 1;

    while (num >= 10) {
        num = num / 10;
        count++;
    }

    return count;
}

答案 3 :(得分:0)

Java:
像下面这样的简单 Java 代码也可以正常工作..!

int num = sc.nextInt();    
int b=Integer.toString(num).length();

C++

to_string(num).length()

C

count = 0    
while (num != 0) {
       num /= 10;   
       count++;
        }