小编Dig*_*can的帖子

阻止用户输入大于int max的值?

我有一些很酷的代码,它采用int值.我让我的小弟弟测试一下,他做的第一件事是什么?他输入了这个:

12345678910
Run Code Online (Sandbox Code Playgroud)

他得到了这个错误:

User did not input a number. (Error Code 1)
Run Code Online (Sandbox Code Playgroud)

那不是真的.有没有办法给他一个"价值太大"的错误?这是我的代码:

try
{
    number = input.nextInt();
}
catch (InputMismatchException e)
{
    System.err.println("User did not input a number. (Error Code 1)");
    System.exit(1);
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

编辑

我使用的已发布的代码已被修改.这是我最终使用的代码,但解决方案不再出现在评论中.

        try 
        {
            double intitalinput = input.nextDouble();

            if (intitalinput > Integer.MAX_VALUE)
            {
                System.err.println("User entered a number larger than " + Integer.MAX_VALUE + ". (Error Code 2)");
                System.exit(2);
            }
            else 
            {
                number = (int) intitalinput;
            }
        }
        catch (InputMismatchException e) 
        {
            System.err.println("User did not input …
Run Code Online (Sandbox Code Playgroud)

java int exception max

2
推荐指数
1
解决办法
6247
查看次数

无法创建文件,因为文件不存在

Java将不会让我createNewFile,因为我想创建不存在,对此,文件,这就是为什么我要创建它.这是我的代码片段.

System.out.println("Please input the path of the install directory.");

System.out.print(">");
installLocation = input.nextLine();

File spreadsheet = new File (installLocation + "diatracker.csv");
File settingsFile = new File (installLocation + "settings.txt");


if ( spreadsheet.exists() )
{
    if ( isValidFile ( spreadsheet.toString() ) )
    {
        //do nothing
    }
    else
    {
        spreadsheet.delete();
        spreadsheet.createNewFile();
    }
}
else
{
    spreadsheet.createNewFile();
}
Run Code Online (Sandbox Code Playgroud)

这是我的错误.

请输入安装目录的路径.

C:\ Users \用户DigiDuncan \桌面\ DiaTracker \

Exception in thread "main" java.io.IOException: The system cannot find the path specified
    at java.io.WinNTFileSystem.createFileExclusively(Native Method) …
Run Code Online (Sandbox Code Playgroud)

java file ioexception

0
推荐指数
1
解决办法
5508
查看次数

Python打印没有在控制台没有明显的原因?

我的程序在网格中绘制框.这是完整的代码:http://pastebin.com/cMEJAAES

这里的想法是我需要一个调试方法.它应该打印乌龟刚绘制的盒子的坐标,它的颜色.由于某种原因,它打印很好,但中间有一个随机的无.这是一些控制台输出的示例:

Reading map data....
Drew tile 1 (1,1)
None as color 3
Drew tile 2 (1,2)
None as color 0
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

#Define our map to draw.
map = [3,           #Set row width.
        3, 0, 0,    #Row data.
        0, 2, 1,
        2, 1, 1]
Run Code Online (Sandbox Code Playgroud)

[...]

def drawmap(m):
#Print the entire map.
    for x in range (0, len(m)):
        if x == 0:
            print("Reading map data....") #Not really, just skipping index 0.
        else:
            draw_tile(map[x])
            lastbox = x
            tileinfo(x)
            move(m, …
Run Code Online (Sandbox Code Playgroud)

python printing variables integer nonetype

-2
推荐指数
1
解决办法
268
查看次数

标签 统计

java ×2

exception ×1

file ×1

int ×1

integer ×1

ioexception ×1

max ×1

nonetype ×1

printing ×1

python ×1

variables ×1