应该不止我一个是在PTA做题遇到了 非零返回 导致不能AC,结果网上搜了一大堆相关资料按照步骤说的来改后依然没什么用的倒霉蛋吧。。这是一件很无助的事情,而且改代码真的很烦……
造成非零返回的原因很多,但是多数情况下或许并不是你的代码有问题,只是你跳的太快PTA跟不上你的速度罢了……文章只讲这种情况下的解决方法:
对此,你只需要回去从头到尾看一下你的代码,是不是有哪些地方太过聪明、复杂、睿智,或者容易造成歧义,找出这部分代码,然后把他们改成简单语句就OK了
另外了解一下什么是非零返回
非零返回是指:我们的程序的一些语法,在PTA执行的过程中抛出了异常,导致程序没能运行到最后一步return,就导致了非零返回。
但是这些语法在其他编译器上是可以通过的,就是在PTA上不行,反正我在IDE上是可以正确运行出结果的
附上实例↓↓↓
这是我修改之前的代码:
import java.util.Scanner;
public class Main {public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int num=in.nextInt();
for(int i=0;i<num;i++) {
if(in.nextInt()==0) {
Student s=new Student(in.nextInt(),in.next(),in.nextInt(),in.next(),in.nextDouble());
System.out.println(s.toString());
}
if(in.nextInt()==1) {
Teacher t=new Teacher(in.nextInt(),in.next(),in.nextInt(),in.next(),in.nextDouble());
System.out.println(t.toString());
}
}
in.close();
}}
class Person{
int id;
String name;
int bir;
Person(int id,String name,int bir) {
this.id=id;
this.name=name;
this.bir=bir;
}
}class Student extends Person{
String major;
double score;
public Student(int id,String name,int bir,String major,double score) {
super(id,name,bir);
this.major=major;
this.score=score;
}
@Override
public String toString() {
return "Student [id="+id+", name="+name+", bir="+bir+", major="+major+", score="+score+"]";
}
}class Teacher extends Person{
String title;
double salary;
public Teacher(int id,String name,int bir,String title,double salary) {
super(id,name,bir);
this.title=title;
this.salary=salary;
}
@Override
public String toString() {
return "Teacher [id="+id+", name="+name+", bir="+bir+", title="+title+", salary="+salary+"]";
}
}
PTA显示“非零返回”

这是我把一步拆解成两步修改简单后的代码
import java.util.Scanner;
public class Main {public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int num=in.nextInt();
for(int i=0;i<num;i++) {
int input=in.nextInt();
if(input==0) {
Student s=new Student(in.nextInt(),in.next(),in.nextInt(),in.next(),in.nextDouble());
System.out.println(s.toString());
}
if(input==1) {
Teacher t=new Teacher(in.nextInt(),in.next(),in.nextInt(),in.next(),in.nextDouble());
System.out.println(t.toString());
}
}
in.close();
}}
class Person{
int id;
String name;
int bir;
Person(int id,String name,int bir) {
this.id=id;
this.name=name;
this.bir=bir;
}
}class Student extends Person{
String major;
double score;
public Student(int id,String name,int bir,String major,double score) {
super(id,name,bir);
this.major=major;
this.score=score;
}
@Override
public String toString() {
return "Student [id="+id+", name="+name+", bir="+bir+", major="+major+", score="+score+"]";
}
}class Teacher extends Person{
String title;
double salary;
public Teacher(int id,String name,int bir,String title,double salary) {
super(id,name,bir);
this.title=title;
this.salary=salary;
}
@Override
public String toString() {
return "Teacher [id="+id+", name="+name+", bir="+bir+", title="+title+", salary="+salary+"]";
}
}
PTA显示答案正确。

为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
所以我开始关注ruby,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998
在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht