cái này tự mày mò chắc còn chổ sai ace góp ý
có cái hàm timso k hiểu sao nó lại sai
[AH]
Mã:
import java.io.*;
import java.util.Scanner;
public class CanBan
{
static int n;
static int a[],b[];
static int i;
private static Scanner scanin;
public static void nhapmang()
{
Scanner scanin = new Scanner(System.in);
System.out.println("nhap so phan tu cua mang");
n = scanin.nextInt();
a = new int[n];
for ( i = 0 ; i < n ; i ++)
{
System.out.println("a[" + i+ "]=");
a[i]=scanin.nextInt();
}
}
public static void xuatmang()
{
for ( i = 0 ; i < n ; i ++)
{
System.out.println("a["+i+"]="+a[i]+"\n");
}
}
public static int tinhtong()
{
int tong = 0;
for ( i = 0 ; i < n ; i ++)
{
tong = a[i] + tong;
}
return tong;
}
public static void chen()
{
int k,tmp;
b = new int[n+1];
scanin = new Scanner(System.in);
System.out.println("nhap vao vi tri muon chen");
k = scanin.nextInt();
System.out.println("\nnhap so muon chen");
tmp = scanin.nextInt();
for ( i = 0 ; i < k ; i++)
{
b[i]=a[i];
}
b[i++]= tmp;
for ( i = k+1 ; i <= n ; i++)
{
b[i]=a[i-1];
}
for ( i = 0 ; i < n+1 ; i ++)
{
System.out.println("b["+i+"]="+b[i]+"\n");
}
}
public static void xoa()
{
int k;
b = new int[n-1];
scanin = new Scanner(System.in);
System.out.println("nhap vao vi tri muon xoa");
k = scanin.nextInt();
for ( i = 0 ; i < k ; i ++)
b[i]=a[i];
for ( i = k + 1 ; i < n ; i++ )
b[i-1]=a[i];
for ( i = 0 ; i < n-1 ; i ++)
System.out.println("b["+i+"]="+b[i]+"\n");
}
public static void timso()
{
Scanner scanin = new Scanner(System.in); System.out.println("\n nhap so muon tim");
tmp = scanin.nextInt();
dem=0;
for ( i = 0 ; i < n ; i ++)
if ( a[i] == tmp)
{
dem++;
System.out.println("\nSo can tim o vi tri "+i);
}
if ( dem == 0 )
System.out.println("Khong co so can tim ");
}
public static void sapxep()
{
int tmp;
int j;
for ( int i = 0 ; i < n-1 ; i ++)
for ( j = i ; j < n ; j ++)
if (a[i] < a[j])
{
tmp = a[i];
a[i]=a[j];
a[j]=tmp;
}
}
public static void main(String[] args)
{
nhapmang();
// xuatmang();
// System.out.println("\n tong la "+tinhtong());
// chen();
// xuatmang();
// xoa();
// timso();
// sapxep();
// xuatmang();
}
}
[/AH]
------------------------------------------------------
đã fix lại hàm timso
có cái hàm timso k hiểu sao nó lại sai
[AH]
Mã:
import java.io.*;
import java.util.Scanner;
public class CanBan
{
static int n;
static int a[],b[];
static int i;
private static Scanner scanin;
public static void nhapmang()
{
Scanner scanin = new Scanner(System.in);
System.out.println("nhap so phan tu cua mang");
n = scanin.nextInt();
a = new int[n];
for ( i = 0 ; i < n ; i ++)
{
System.out.println("a[" + i+ "]=");
a[i]=scanin.nextInt();
}
}
public static void xuatmang()
{
for ( i = 0 ; i < n ; i ++)
{
System.out.println("a["+i+"]="+a[i]+"\n");
}
}
public static int tinhtong()
{
int tong = 0;
for ( i = 0 ; i < n ; i ++)
{
tong = a[i] + tong;
}
return tong;
}
public static void chen()
{
int k,tmp;
b = new int[n+1];
scanin = new Scanner(System.in);
System.out.println("nhap vao vi tri muon chen");
k = scanin.nextInt();
System.out.println("\nnhap so muon chen");
tmp = scanin.nextInt();
for ( i = 0 ; i < k ; i++)
{
b[i]=a[i];
}
b[i++]= tmp;
for ( i = k+1 ; i <= n ; i++)
{
b[i]=a[i-1];
}
for ( i = 0 ; i < n+1 ; i ++)
{
System.out.println("b["+i+"]="+b[i]+"\n");
}
}
public static void xoa()
{
int k;
b = new int[n-1];
scanin = new Scanner(System.in);
System.out.println("nhap vao vi tri muon xoa");
k = scanin.nextInt();
for ( i = 0 ; i < k ; i ++)
b[i]=a[i];
for ( i = k + 1 ; i < n ; i++ )
b[i-1]=a[i];
for ( i = 0 ; i < n-1 ; i ++)
System.out.println("b["+i+"]="+b[i]+"\n");
}
public static void timso()
{
Scanner scanin = new Scanner(System.in); System.out.println("\n nhap so muon tim");
tmp = scanin.nextInt();
dem=0;
for ( i = 0 ; i < n ; i ++)
if ( a[i] == tmp)
{
dem++;
System.out.println("\nSo can tim o vi tri "+i);
}
if ( dem == 0 )
System.out.println("Khong co so can tim ");
}
public static void sapxep()
{
int tmp;
int j;
for ( int i = 0 ; i < n-1 ; i ++)
for ( j = i ; j < n ; j ++)
if (a[i] < a[j])
{
tmp = a[i];
a[i]=a[j];
a[j]=tmp;
}
}
public static void main(String[] args)
{
nhapmang();
// xuatmang();
// System.out.println("\n tong la "+tinhtong());
// chen();
// xuatmang();
// xoa();
// timso();
// sapxep();
// xuatmang();
}
}
[/AH]
------------------------------------------------------
đã fix lại hàm timso