正如问题标题所示,我无法理解为什么我的排序方法对于单独的输入表现不同
问题约束:字符串数组用于输入n
个数字,然后按降序重新排列它们,如果遇到具有相同值的数字,如{{1} }}和000.0
,它们应按照我输入的顺序列出,并且应以与输入格式相同的格式输出。
我的问题:.0
当我输入n=2
时,我的代码输出0.12,.12
,但是当我输入时0.12, .12
n=3
1, 0.12,.12
1,.12, 0.12
1}}它输出1,0.12,.12
(而不是String[] s= new String[n];
//input :
for(int i=0;i<n;i++)
s[i]=sc.next();
//Sorting :
for(int i=0;i<n;i++)
{
BigDecimal d=new BigDecimal(s[i]);
for (int j =i+1; j<n; j++)
{
BigDecimal a=new BigDecimal(s[j]);
if(d.compareTo(a)==-1)
{
String m = s[j];
s[j]=s[i];
s[i]=m;
}
}
}
//Output :
for(int i=0;i<n;i++)
System.out.println(s[i]);
)所以,为什么会出现这种情况?我的代码中的任何细微修改都可以纠正这个故障吗?
代码:
compareto
处理Bigdecimal
类型编号时0.12
方法认为.12
和String
相等,但处理.12
类型变量时不考虑{{} 1}}和0.12
相同,所以只比较我将它们转换为Bigdecimal
类型,但是为了显示我将它们保持为Strings
,因为Bigdecimal
类型舍入.12
1}}为0.12
但Strings
没有。
答案 0 :(得分:0)
使用测试代码似乎工作正常:
<?php
if(isset($_GET['sid'])){
echo'
<div class="right_col" role="main">';
$a=$_GET['sid'];
$update = $obj->selectdata($a);
foreach($update as $upd)
{
$upd['sname'];
echo'
<h1 class="text-center">update student</h1>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-8">
<form class="form-horizontal" action="dashboard.php?sid='.$upd['id'].'" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" value="'.$upd['sname'].'" name="sname" required class="form-control"/>
</div>
<div class="form-group">
<label>Father Name</label>
<input type="text" value="'.$upd['sfname'].'" name="sfname" required class="form-control"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" value="'.$upd['email'].'" name="semail" required class="form-control"/>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="spass" placeholder="Change Password" required class="form-control"/>
</div>
<div class="form-group">
<label>Date Of Birth</label>
<input type="date" value="'.$upd['dob'].'" name="sdob" required class="form-control"/>
</div>
<div class="form-group">
<label>Gender</label>
<div class="radio-inline">
if($upd['gender']== "Male") {
echo '
<label><input cheacked= "cheacked" type="radio" name="sgender" value="Male" />Male</label>
</div>';
}
else {
echo' <div class="radio-inline">
<label><input type="radio" cheacked= "cheacked" name="sgender" value ="Female" />Female</label>
</div></div>';
}
<div class="form-group">
<label>Contact</label>
<input type="text" name="scontact" value="'.$upd['contact'].'" required class="form-control"/>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="saddress" value="'.$upd['address'].'" required class="form-control"/>
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" name="spic" required class="form-control"/>
</div>
<button type="submit" name="upt_form" class="btn btn-default">Submit</button>
</form>
</div>
</div>';
}
}
else {
echo'
<div class="right_col" role="main">
<h1 class="text-center">add student</h1>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-8">
<form class="form-horizontal" action="dashboard.php" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" name="sname" required class="form-control"/>
</div>
<div class="form-group">
<label>Father Name</label>
<input type="text" name="sfname" required class="form-control"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="semail" required class="form-control"/>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="spass" required class="form-control"/>
</div>
<div class="form-group">
<label>Date Of Birth</label>
<input type="date" name="sdob" required class="form-control"/>
</div>
<div class="form-group">
<label>Gender</label>
<div class="radio-inline">
<label><input type="radio" name="sgender" value="Male" required/>Male</label>
</div>
<div class="radio-inline">
<label><input type="radio" name="sgender" value ="Female"required />Female</label>
</div></div>
<div class="form-group">
<label>Contact</label>
<input type="text" name="scontact" required class="form-control"/>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="saddress" required class="form-control"/>
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" name="spic" required class="form-control"/>
</div>
<button type="submit" name="stu_form" class="btn btn-default">Submit</button>
</form>
</div>
</div>';
}
?>
你可以在这里看到: