Java 获取字符串中的空值

Java 获取字符串中的空值,java,Java,刚开始使用java。 主代码中没有问题。 hihestPage和LOWESTPAGE都显示正确的值 但是我在bookmaxpage和bookminpage上的值都是空的。 正在尝试获取最大和最小页面上的标题名称 package Library; 导入java.util.Scanner; 导入java.util.array 公共班级图书馆{ public static void main(String[] args) { // TODO Auto-generated method

刚开始使用java。 主代码中没有问题。 hihestPage和LOWESTPAGE都显示正确的值 但是我在bookmaxpage和bookminpage上的值都是空的。 正在尝试获取最大和最小页面上的标题名称

    package Library;
导入java.util.Scanner; 导入java.util.array

公共班级图书馆{

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner scan = new Scanner(System.in);
    
    int i,j,number;
    int count=0;
    float highestPrice=0,total=0;
    String bookmaxpage=" ",bookminpage=" ";
    int highestPage=0;
    float averageCost=0;

    String Title[] = new String[20];
    String Author[] = new String[20];
    String Publisher[] = new String[20];
    float Price[] = new float[20];
    int Page[] = new int[20];
    int ISBN[] = new int[20];
    
    System.out.println("Enter the number of books that u want to enter : ");
    number = scan.nextInt();
    
    for(i=0;i<number;i++) {
        
        System.out.println("Enter Details of the book. ");
                    
        System.out.println("Enter the title: ");
        Title[i]=scan.next();
            if(Title[i].equalsIgnoreCase("nomore")) {
                break;
            }
        System.out.println("Enter the author: ");
        Author[i]=scan.next();
        System.out.println("Enter the publisher: ");
        Publisher[i]=scan.next();
        System.out.println("Enter the price: ");
        Price[i]=scan.nextFloat();  
        System.out.println("Enter the pages: ");
        Page[i]=scan.nextInt();
        System.out.println("Enter the ISBN: ");
        ISBN[i]=scan.nextInt();
        
        total=total+Price[i];
        count++;
        
        
    }
    
    for(i=0;i<Price.length;i++) {
        if(Price[i]>highestPrice)
            highestPrice = Price[i];
    }

    
    float lowestPrice= Price[0];
    for(i=0;i<Price.length;i++) {
        if(lowestPrice<Price[i])
            lowestPrice = Price[i];
            
    }
    
    for(i=0;i<Page.length;i++) {
        if(Page[i]>highestPage)
            highestPage = Page[i];
        bookmaxpage=Title[i];
            
    }
    
    int lowestPage= Page[0];
    for(i=0;i<Page.length;i++) {
        if(lowestPage<Page[i])
            lowestPage = Page[i];

        bookminpage=Title[i];
    }
        
    
    averageCost = total / number;
            
    
    
    
    System.out.println("Title \t\t Author \t\t Publisher \t\t Price \t Pages \t ISBN");
    System.out.println("======\t\t ====== \t\t ========= \t\t ===== \t ===== \t ====");
    for(i=0;i<number;i++) {
        
            System.out.println(Title[i] +" \t\t "+ Author[i] +" \t\t\t "+ Publisher[i] +" \t\t\t "+ Price[i] +" \t "+ Page[i] +" \t "+ ISBN[i]);
    }
    

    System.out.println("\n\n\n\nTotals ");
    System.out.println("------------------------------");
    System.out.println("Total number of books : " + count);
    System.out.println("Total cost of books : " + total);
    System.out.println("Maximum cost of a book : " + highestPrice);
    System.out.println("Minimum cost of a book : " + lowestPrice);
    System.out.println(bookmaxpage + " has the highest number of pages :" + highestPage);
    System.out.println(bookminpage + " has the lowest number of pages :" + lowestPage);
    System.out.println("Average Cost of books : " + averageCost);
}
publicstaticvoidmain(字符串[]args){
//TODO自动生成的方法存根
扫描仪扫描=新扫描仪(System.in);
int i,j,数字;
整数计数=0;
浮动最高价格=0,总计=0;
字符串bookmaxpage=“”,bookminpage=“”;
int highestPage=0;
浮动平均成本=0;
字符串标题[]=新字符串[20];
字符串作者[]=新字符串[20];
字符串发布器[]=新字符串[20];
浮动价格[]=新浮动[20];
int Page[]=新int[20];
整数ISBN[]=新整数[20];
System.out.println(“输入您想要输入的图书数量:”;
number=scan.nextInt();

对于(i=0;i此代码存在一些问题,但您特别指出的问题来自于分配
bookminpage
bookmaxpage
的循环

因为您正在初始化数组,使其包含20个元素,所以循环一直运行到
i=19
,因此除非您恰好输入20本书,否则您将设置
bookminpage=Title[19]=null

我建议在您询问将有多少本书之后初始化您的数组


我还认为您的
if
语句中缺少了一些大括号

if(condition)
    statement1;
statement2;
那么只有
语句1
if
块中



作为旁注,我将研究一些Java命名约定,变量名应该几乎总是在
camelCase

标题和页面数组的内容中。另外,请更正发布代码中的任何编译错误(
bookminpage
似乎没有声明)ty以便更正。字符串标题[]=新字符串[20];整型页面[]=新整型[20];这是声明。请确保编辑文章时添加了内容,而不仅仅是在评论中添加内容!您还必须在某个地方填充这些数组?是否也包含这些内容?您可以在问题中发布完整的代码吗?很难帮助您处理随机片段!那么我是否应该更改…[20]数组到用户在int number中输入的数字?确切地说,如果您输入的书籍超过20本,数组大小不正确甚至会导致崩溃,应谨慎使用固定大小的数组。不知道这会产生这样的错误。非常感谢您的时间。没问题,我还添加了另一个示例你可能误解了什么?把'bookminpage=title[i]'放在if语句中不会解决空值问题吗?如果只有15本书,那么第18本书不能再大,因此不会给bookminpage分配空值。